DEV Community

Cover image for Pure functions in javaScript
Hamza Ali
Hamza Ali

Posted on

Pure functions in javaScript

Functions

People think that computer science is the art of geniuses but the actual reality is the opposite, just many people doing things that build on each other, like a wall of mini stones.Donald Knuth

Functions are bread and butter of JavaScript programming. The concept of warping a piece of code in a block has many values. In JavaScript functions are first class citizens. First class citizen basically mean that functions are able to do everything that other can.

  • Functions can be assigned to a variable

  • Function can be passed as an argument

  • Function can be returned from a function

A program is a collection of blocks of code that are called functions. Functions in programming are same as function in Math let take an example
In Math we write functions like this f(x)=x+2
This simply means that a function takes x as an input and return x+2. lets now create a program if the above function .

function addTwo(x){
return x+2
}
Enter fullscreen mode Exit fullscreen mode

The addTwo is the programming implementation of f(x)=x+2 this function . The addTwo function is a Pure function

Pure Function

For a function to be pure it have to meet the following two conditions,

  • *** Given the same input the same output should be returned. ***

  • No side effects.

Lets now try to defend our statement about the addTwo function being pure. we will have to check if the addTwo function meets the above mentioned conditions if it does then we will be successfull in defending our statment.

*** Given the same input the same output should be returned ***
lets pass 2 as an input.

console.log(addTwo(2))
 It will return 4
Enter fullscreen mode Exit fullscreen mode

Now it does't matter how many times we pass the value of 2 the function addTwo will always return 4. So it is clear that our addTwo function satesfies the first condition of Pure function.

Lets now try to verify the second condition.

*** No Side Effects **
what is side effect?
When a function modifies or relies on anything outside or its parameters then the function has side effects.

The above function of ours is not using anything outside of its parameter which means that it setisfies the second condition of pure function.

It is concluded that our *** addTwo function is a pure function***

A principle of identifying a pure function.

If calling a function without using its return value makes sense then it is not a pure function.

Why you should use pure functions?

  • Easy to refactor and move around.
  • Independent of outside world.
  • Simplest reusable block of code.
  • Immune to bugs outside of its scope.
  • Makes programs more adaptable to future changes

Conclusion

A computer program is a collection of small blocks of code that are called function.
Pure functions are function that when given same input will return the same output and pure function don't have side effects. Pure functions are independent of out side and can easily be refactored.

Top comments (0)