2. First-Class Objects

ajaykumbhare

Ajay kumbhare

Posted on September 23, 2021

2. First-Class Objects

Functions in JavaScript are first-class objects which means they can be stored in variables, objects, or in the array.

We can work with functions in JavaScript in many different ways

  • We can store functions in variables
const professor = function () {
  console.log("Let's do heist again...");
};
Enter fullscreen mode Exit fullscreen mode
  • We can pass as an argument to a function
const filter = (array, callback) => {
  return array.filter(callback);
};

const isEven = (num) => num % 2 === 0;

filter([1, 2, 3, 4, 5], isEven);
Enter fullscreen mode Exit fullscreen mode
  • We can return a function from another function
const doSum = (a) => (b) => a + b;
Enter fullscreen mode Exit fullscreen mode

The above code is an example of the curry function that will discuss in the next blog. but if you see it returns a function so when you call doSum(10) it will return (b) => a + b function and it will expect one more argument.

  • We can store function inside an objects
const bank = {
  balance: () => 100000
};
Enter fullscreen mode Exit fullscreen mode

I am learning FP. If you feel I am wrong then please feel free to write a comment in the comment box so I will update the document.

💖 💪 🙅 🚩
ajaykumbhare
Ajay kumbhare

Posted on September 23, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

2. First-Class Objects
javascript 2. First-Class Objects

September 23, 2021