What is an example of an impure function in JavaScript

user2167582 picture user2167582 · Mar 6, 2016 · Viewed 9.1k times · Source

Having seen a lot of pure functions and how they have no side effects, what would be an example of an impure function, which is always been antagonized as unstable and major source of error?

Answer

crackmigg picture crackmigg · Mar 6, 2016

For example an impure function that has a side effect on a variable outside of its own scope:

var count = 0;

function increaseCount(val) {
    count += val;
}

Or a function that returns different values for the same input because it evaluates a variable that is not given as parameter:

var count = 0;

function getSomething() {
    return count > 0;
}