Pure Function in JavaScript

In JavaScript, a pure function is a function that always returns the same output for the same input, and does not modify any external state or variables outside of its scope.

Here's an example of a pure function:

function add(a, b) {
  return a + b;
}

This function takes two parameters a and b, and returns their sum. It does not modify any external state or variables, and always returns the same output for the same input.

On the other hand, here's an example of an impure function:

let c = 0;

function addImpure(a, b) {
  c = a + b;
  return c;
}

This function takes two parameters a and b, adds them together, and stores the result in the external variable c. This function modifies the external state, and its output depends on the current value of c, so it is not a pure function.