Latest Code Tutorials

Pure Functions In Javascript


Hello, Developers.  In today’s AppDividend Tutorial, I have briefly described Pure Functions In Javascript.

Pure functions in Javascript are kind of Functions where the return value is determined by its parameter passed at call time.

Javascript is not a purely functional language; it treats functions as objects (first-class functions).

If you provide some input parameter value to the function and it always returns the same result, then It’s Pure Functions in Javascript.

Pure Functions

Pure Functional Programming concept is derived from this two strict functional programming Language.

  1. Clojure
  2. Scala

Pure Functions strictly does not rely on particular Ajax call or any Database call. It simply takes a parameter and performs some calculation provides in function body and return it.

It solely depends upon the argument pass it into the functions call.

Pure Functions In Javascript
Pure Functions In Javascript

It only maps the input values to the output values.

You can call this programming style as a Procedural Programing because It is a sequence of operations performs on the input to that function.

The function can access only those variables that are pass to that function as a parameter.

A global variable or an instance of a class is not considered as a legitimate variable inside any Pure Functions.

The global scope of a variable is not used in the producing of a Pure Functions.

It’s set of instruction that behaves as a Mathematical Functions.

It’s a role-based function whose only task is to take an argument, perform some operations on that variable and then return it.


  1. You can always predict the result of a given function.
  2. Testing is always easy because It does not mutate the state of a variable outside of its lexical scope.
  3. Functional programming reduces the line of codes and almost get rid of a loop.




let pure = (y) => {
    let x = 10;
    x = x + y;
    return x;

let y = pure(3);
console.log(y); // 13

let z = pure(3);
console.log(z); // 13

If you directly run above code in the browser, then you might face the following issue.

Possible Errors

  1. You can get any syntax error.
  2. If you perform code directly in your browser, then chances are very high to fail the webpack compilation process.

Possible Solutions

  1.  Beginner’s Guide To Setup ES6 Development Environment  Follow this article strictly and put above code in the main.js file

In above example, every time at call time of the function, when we pass the parameter as three then It will always give us 13 in return in all the circumstances. So it’s a Pure Javascript Function.

In JavaScript, simple variables like numbers, strings and booleans are immutable. However, data structures like objects and arrays are mutable because it is a collection of data.


const numbers = [1, 2, 3];
numbers.forEach(x => console.log(x)); //1 2 3

Arrays and Objects can be mutated because It’s a non-primitive data type.

Above example simple print the value of an element in an array one by one so It’s a Pure Javascript Function.

Example #1


let arr = [1, 2, 3, 4, 5, 6];

even = arr.filter(function (element) {
    return element%2 === 0;
console.log(even); //[2,4,6]

If you do not mutate an old state of an array, then It is also called Pure Functions.

In above example, Filter function just checks the condition and if it satisfies than It returns the new set of an array.

Example #2


let dances = [
    {name: 'Krunal', type: 'Robot'},
    {name: 'Ankit', type: 'Salsa'},
    {name: 'Tom', type: 'Garba'},
    {name: 'Jerry', type: 'Belly'}
let collection = dances.filter((dance)=>{
   return  dance.type === 'Robot'
console.log(collection); // [object]

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.