Pure functions are self-contained, and have no side effects. Given the same set of inputs, a pure function will always return the same output value.
The following function is pure:
function pure(data) {
return data.total + 3;
}
However, this function is not pure as it modifies an external variable:
function impure(data) {
data.total += 3;
return data.total;
}
Example:
data = {
total: 6
};
pure(data); // outputs: 9
impure(data); // outputs: 9 (but now data.total has changed)
impure(data); // outputs: 12