2. First-Class Objects
Ajay kumbhare
Posted on September 23, 2021
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...");
};
- 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);
- We can return a function from another function
const doSum = (a) => (b) => a + b;
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
};
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.
💖 💪 🙅 🚩
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.