user2167582
user2167582

Reputation: 6378

What is an example of an impure function in JavaScript

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?

Upvotes: 17

Views: 11800

Answers (6)

Ted Hopp
Ted Hopp

Reputation: 234847

Math.random() is an impure function; it changes the internal state of the Math object so you get different values on successive calls. console.log() and alert() are impure functions because they have side effects (although they generate the same behavior and always return the same value for identical calls).

Any function that changes the internal state of one of its arguments or the value of some external variable is an impure function. This includes closures where calling a method changes the internal state of the closure itself:

let nextInt = (function() {
    var i = 0;
    return function() {
        return i++;
    };
})();

let a = nextInt(); // sets a to 0
let b = nextInt(); // sets b to 1
                   // etc.

Where'd you get the idea that impure functions are always considered a bad thing?

Upvotes: 3

abhiagNitk
abhiagNitk

Reputation: 1067

A pure function doesn’t depend on and doesn’t modify the states of variables out of its scope.

Concretely, that means a pure function always returns the same result given same parameters. Its execution doesn’t depend on the state of the system.

var values = { a: 1 };

function impureFunction ( items ) {
  var b = 1;

  items.a = items.a * b + 2;

  return items.a;
}

var c = impureFunction( values );
// Now `values.a` is 3, the impure function modifies it.

Here we modify the attributes of the given object. Hence we modify the object which lies outside of the scope of our function: the function is impure.

var values = { a: 1 };

function pureFunction ( a ) {
  var b = 1;

  a = a * b + 2;

  return a;
}

var c = pureFunction( values.a );

we simply modify the parameter which is in the scope of the function, nothing is modified outside!

var values = { a: 1 };
var b = 1;

function impureFunction ( a ) {
  a = a * b + 2;

  return a;
}

var c = impureFunction( values.a );
// Actually, the value of `c` will depend on the value of `b`.
// In a bigger codebase, you may forget about that, which may 
// surprise you because the result can vary implicitly.

Here, b is not in the scope of the function. The result will depend on the context: surprises expected!

var values = { a: 1 };
var b = 1;

function pureFunction ( a, c ) {
  a = a * c + 2;

  return a;
}

var c = pureFunction( values.a, b );
// Here it's made clear that the value of `c` will depend on
// the value of `b`.

Reference : For more details, click here

Upvotes: 7

Davit Tvildiani
Davit Tvildiani

Reputation: 1965

  1. Impure functon is a function which returns different result for same input parameters, that is to say that it depends on some state;
  2. Function that may return same result for same input parameters, but that mutates other state object. object that it does not depends but others do // causes side effects

example:

1)

 var toggled = false; /* state */

    /*impure function*/
    function  impureFn(number){
     if(number%2 == 0)
      toggled = true;
     return toggled;
    }

    /*Execute this in order */
    impureFn(5) // returns false
    impureFn(2) //returns true
    impureFn(5) // now returns true

Upvotes: 1

crackmigg
crackmigg

Reputation: 5901

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;
}

Upvotes: 22

Dmitriy
Dmitriy

Reputation: 3765

pure function (get argument and return new value):

function (objectArgument) {
   return {
       value: objectArgument.value + 123,
       // other arguments
   };
}

impure function (get argument, modified it and return modified object):

function (objectArgument) {
   objectArgument.value += 123;
   return objectArgument;
}

Upvotes: 0

Scimonster
Scimonster

Reputation: 33409

An example i can think of (that is indeed quite confusing) is Array#reverse(). Instead of returning a new array, it modifies it in place while returning the original array.

Some other array functions do this as well, such as splice, push, shift, pop, unshift.

Upvotes: 0

Related Questions