JS Intermediate – What is Functional Programming in Javascript

Functional Programming

Functional Programming is a programming paradigm – a style of building the structure band element of computer programs that treats computation as the evaluation of mathematical function and avoids changing-state and mutable data.

JavaScript functions may be employed to carry out a broad range of operations, including verifying user input, modifying strings or arrays, producing random numbers, and connecting with online APIs. By breaking down complex processes into smaller, more manageable chunks, functions may also be used to make your code more modular, easier to understand, and easier to maintain.

Higher-order functions, or those that accept other functions as inputs or return other functions as values, are also supported by JavaScript. For operations like sorting arrays, data filtering, or mapping values to new arrays, higher-order functions are frequently utilized.

The articles in this JavaScript Intermediate series are listed below.

There are three main terms for functional programming:-

  • Pure Function
  • First-class Function
  • Higher Order Function

Pure Function

A function will be called a Pure Function if these two conditions will satisfy:-

  • It returns the same result if given the same arguments
  • It doesn’t cause any observable side effects.

Example:

function sqr(n){
   return n*n
}
 
console.log(sqr(4));
console.log(sqr(4));
console.log(sqr(4));

Output:

16
16
16

In this example, no matter what the number is if the argument is the same the function will return the same result, and this block of code will not bother the entire code.

First-class Function

A function will be called a First-class Function if these conditions will satisfy:-

  • A function can be stored in a variable

Example:

function add(a, b){
   return a+b
}
 
var sum = add
console.log(sum(20, 33));
console.log(typeof(sum));	// We can check the type of the variable

Output:

53
function
  • A function can be stored in an array

Example:

function add(a, b){
   return a+b
}
 
var sum = []
sum.push(add)
console.log(sum);
console.log(sum[0](81, 34));

Output:

115
  • A function can be stored in an object

Example:

function add(a, b){
   return a+b
}
 
var obj = {
   sum:add
}
console.log(obj);
console.log(obj.sum(11, 34));

Output:

{ sum: [Function: add] }
45
  • We can create functions as we need

Example:

setTimeout(function(){
   console.log('A function inside another.');
}, 1000)

Output:

A function inside another.

Higher Order Function

In javascript, functions can be passed into another function as parameters or returned from them as well. A function that accepts another function as a parameter or can return another function is called a higher-order function.

Example:

function add(a, b) {
 // a function that will pass through the outer function
 return a + b;
}
 
function outer(a, b, func) {
 var c = a + b;
 var d = a - b;
 
 function multiply() {
   //a function inside a function
   var e = func(a, b);
   return c * d * e;
 }
 return multiply; /// Outer function return a function
}
 
var result = outer(5, 3, add);

Output:

128

Callback Function

In javascript, A function can be invoked before defining it. When a function passes through another function as a parameter and invokes anywhere in it then the passed-through function is called the callback function.

This technique allows a function to call another function and run after another function has finished.

Example:

function add(a, b, callback) {
   var c = a+b
   console.log('The sum of '+ a + ' and ' + b + ' is ' + c);
   callback();
}
function endings() {
   console.log('Addition Done.');
}
 
add(45, 67, endings)

Output:

The sum of 45 and 67 is 112
Addition Done.

Return A Function From Another Function

In a nested function if a function returns another function then the second function is stored in the first one by closure method.

Example:

function greet(msg) {
   function greetings(name) {
       return msg + ', ' + name + '!'
   }
   return greetings
}
 
var gm = greet('Good Morning')
console.log(gm);
 
var msg = gm('Geeks')
console.log(msg);

Output:

[Function: greetings]
Good Morning, Geeks!

Note: In this example, returned function greetings are stored in “var gm” as a function. To see the proper result it should be invoked later which is stored in the msg variable shown as an example.

Recursive Function

If a function invokes it by itself until satisfying a terminating condition then the function is called a recursive function.

A recursive function must have at least one terminating condition where it stops calling itself, or the function will call itself indefinitely until JavaScript throws an error.

Example:

function fact(n) {
 if (n == 1) {
   return 1;
 }
 return n * fact(n - 1);
}
 
console.log(fact(5));

Output:

120
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.