Pure Functions :

Pure Functions :

  1. It takes an argument
  2. It should return something
  3. for same input you should get same out put
  4. the result should be influenced by outer parameter
  5. it should not mutate the original argument

Mutating : Changing the Value

  1. pure function program :

function getDoubleValue(number){    //Pure functions program 
 return number * 2;
}

const result = getDoubleValue(225250);
console.log(result);

OP : 450500

Impure Functions :

const  x=4;   //this is impure function due to it depends upon outer parameter
function getDoubleValue(number){    //imPure functions program 
 return number * 2 * x;
}

const result = getDoubleValue(225250);
console.log(result);

Imp : A Pure Function is a Function which Does Everything Internally

Append Numbers :

function appendNumbers(arr){  //appendNumbers Using pure funcitons 
   let newArr = [];
   newArr.push(...arr, 5,6);
    return newArr;  
    
}

const result = appendNumbers([1,2,3,4]);
console.log(result);

Key Points : When you are writing functions, try to write with in, not from outer prameters!

First Class Functions :

a programming language is said to have first class functions in that language are treated like other variables so the functions can be assigned to any other variable or passed as an argument or can be returned by another function.

It means that functions in JavaScript have the same status as other data types like strings, numbers, or objects. Functions can be:

  1. Assigned to variables
  2. Passed as arguments to other functions
  3. Returned from other functions

Method : 1