What is Pure function in javascript ES6 ?

A pure function in JavaScript, ES6, or any other version, is a function that, given the same input, will always return the same output and has no side effects. Here are the key characteristics of pure functions:

  1. Deterministic: A pure function will always produce the same output for the same set of inputs, regardless of when or where it is called.
  2. No Side Effects: A pure function doesn’t modify external state or have observable side effects. It doesn’t change the input values or any variables outside its scope. This makes pure functions easier to reason about and test.

Here’s an example of a pure function:

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

// Examples of pure function calls
console.log(add(2, 3)); // Output: 5
console.log(add(2, 3)); // Output: 5

// Impure function with side effect
let result = 0;

function impureAdd(a, b) {
  result = a + b;
  return result;
}

// Example of impure function call
console.log(impureAdd(2, 3)); // Output: 5
console.log(result); // Output: 5 (side effect observed)

In the above example, the add function is pure because it only depends on its input parameters, and it doesn’t modify any external state. On the other hand, the impureAdd function is impure because it modifies the external variable result and has a side effect.

The benefits of using pure functions include:

  • Predictability: Since pure functions are deterministic, you can predict their behavior based on input, making the code easier to understand.
  • Testability: Pure functions are easy to test because they don’t rely on external state or dependencies.
  • Debugging: Debugging is simpler because the behavior of a pure function is isolated and doesn’t depend on external factors.

In functional programming, the use of pure functions is encouraged because it promotes a declarative and predictable coding style, making it easier to reason about the behavior of the code.

A pure function is a function that returns a value that is computed based on its arguments. Pure functions take at least one argument and always return a value or another function. They do not cause side effects, set global variables, or change anything about application state. They treat their arguments as immutable data

Example of Impure function

selfEducate function is not a pure function. It does not take any arguments, and it does not return a value or a function. it also modify a variable outside of its scope e.g frederick and this is causes side effect

var frederick = {
 name: "Frederick Douglass",
 canRead: false,
 canWrite: false
}
const selfEducate = (person) => {
 person.canRead = true
 person.canWrite = true
 return person
}
console.log( selfEducate(frederick) )
 // {name: "Frederick Douglass", canRead: true, canWrite: true}
console.log( frederick )
 // {name: "Frederick Douglass", canRead: true, canWrite: true}


Example of Pure Function

const frederick = {
 name: "Frederick Douglass",
 canRead: false,
 canWrite: false
}
const selfEducate = person =>
 ({
 ...person,
 canRead: true,
 canWrite: true
 })
console.log( selfEducate(frederick) )
console.log( frederick )
// {name: "Frederick Douglass", canRead: true, canWrite: true} 
// {name: "Frederick Douglass", canRead: false, canWrite: false}

this version of selfEducate is a pure function. It computes a value based on the argument that was sent to it: the person. It returns a new person object without mutating the argument sent to it and therefore has no side effects.

Pure functions are naturally testable. They do not change anything about their environment or “world,” and therefore do not require a complicated test setup or teardown. Everything a pure function needs to operate it accesses via arguments. When testing a pure function, you control the arguments, and thus you can estimate the outcome

Leave a Comment