What are First-class Functions?
In JavaScript and other functional languages, functions can also be assigned to variables and you can pass them around, just like they were variables.
First-class function are functions that you can:
- Pass as arguments to other functions
- Store them in a variable
- Return as values from other functions
Take the following as an example:
Copied to clipboard! Playground
const greet = function () {
console.log('π');
}
// The greet variable is now a function, we can invoke it
greet();
Here the greet
function is passed around and is treated like a variable.
Resources:
π More Webtips
Master the Art of Frontend
- Access 100+ interactive lessons
- Unlimited access to hundreds of tutorials
- Prepare for technical interviews