First-class Function
A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function, and can be assigned as a value to a variable.
Example | Assign a function to a variable
const foo = function() {
console.log(“foobar”);
}
foo(); // Invoke it using the variable
// foobar
An Anonymous Function in a Variable has been assigned then, we used that variable to invoke the function by adding parentheses () at the end.
Note: Even if your function was named, you can use the variable name to invoke it. Naming it will be helpful when debugging your code. But it won’t affect the way we invoke it.
Example | Pass a function as an Argument
function sayHello() {
return “Hello, “;
}
function greeting(helloMessage, name) {
console.log(helloMessage() + name);
}
// Pass `sayHello` as an argument to `greeting` function
greeting(sayHello, “JavaScript!”);
// Hello, JavaScript!
Passing our sayHello() function as an argument to the greeting() function explains how we are treating the function as a value.
Note: The function that we pass as an argument to another function, is called a Callback function. sayHello is a Callback function.
Example | Return a function
function sayHello() {
return function() {
console.log(“Hello!”);
}
}
In this example; We need to return a function from another function – We can return a function because we treated a function in JavaScript as a value.
Note: A function that returns a function is called a Higher-Order Function.
Back to our example; Now, we need to invoke the say Hello function, and it returned Anonymous Function. To do so, we have two ways:
1- Using a variable
const sayHello = function() {
return function() {
console.log(“Hello!”);
}
}
const myFunc = sayHello();
myFunc();
// Hello!
This way, it returns the Hello! message.
Note: You have to use another variable. If you invoked sayHello directly, it would return the function itself without invoking its returned function.
2- Using double parentheses
function sayHello() {
return function() {
console.log(“Hello!”);
}
}
sayHello()();
// Hello!
We are using double parentheses ()() to invoke the returned function as well.
What is the first-class function in JavaScript?
First-Class Function: A programming language is said to have First-class functions if 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. JavaScript treat function as a first-class-citizens. This means that functions are simply a value and are just another type of object.
Example: Let us take an example to understand more about the first-class function.
Javascript
<script>
const Arithmetics = { add: (a, b) => { return `${a} + ${b} = ${a + b}`; }, subtract: (a, b) => { return `${a} – ${b} = ${a – b}` }, multiply: (a, b) => { return `${a} * ${b} = ${a * b}` }, division: (a, b) => { if (b != 0) return `${a} / ${b} = ${a / b}`; return `Cannot Divide by Zero!!!`; }
}
document.write(Arithmetics.add(100, 100) + “<br>”); document.write(Arithmetics.subtract(100, 7) + “<br>”); document.write(Arithmetics.multiply(5, 5) + “<br>”); document.write(Arithmetics.division(100, 5)); </script> |
Note: In the above example, functions are stored as a variable in an object.
Output:
100 + 100 = 200
100 – 7 = 93
5 * 5 = 25
100 / 5 = 20
Example 2:
Javascript
<script>
const Geek = (a, b) => { return (a + ” ” + b); }
document.write(Geek(“Soumya”, “Jindal”)); </script> |
Output:
Soumya Jindal
For classes of JavaScript – “KNOW JAVASCRIPT AT THE BACK OF YOUR HAND”. Your free source of Information and for my other blogs and updates do follow https://topguestposting.com/author/pro-book/