Product of two number using HOC - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

HOC

HOC or Higher Order Functions in JavaScript are a special type of functions that receives another function as argument or have a function set as their return value or do both. HOC along with closures is a very powerful tool in JavaScript.

We are required to write a JavaScript Higher Order Function that can be used to obtain the product of two numbers.

Example

Following is the code −

const num1 = 24;
const num2 = 5;
const productHOC = num1 => {
   return product = num2 => {
      return num1 * num2;
   };
};
console.log(productHOC(num1)(num2));

Output

Following is the output in the console −

120
raja
Published on 16-Sep-2020 13:18:45
Advertisements