re: Pure Array Modifications: Plain JavaScript vs. Modern JavaScript VIEW POST

TOP OF THREAD FULL DISCUSSION
re: I understend How the ++ operator Works, but I believe by changing a parameter value, the function will no longer be a pure function. I think that t...

Nope, the function will remain pure because primitive types in JavaScript are passed by value. This means when i is passed in as an argument, pureInsert creates a brand new functional context that contains its own copy of i as opposed to referencing the original variable that was passed in. Since the function cannot mutate the original variable, purity is maintained.

let myNum = 1

const attemptMutation = (i) => {
  console.log(++i)
}

console.log(myNum)      // logs 1
attemptMutation(myNum)  // logs 2
console.log(myNum)      // still logs 1

If you really wanted to make the ++ operator "impure", you could use a higher-order function.

code of conduct - report abuse