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.
JS example:
const foo = function() {
console.log("foobar");
}
// Invoke it using the variable
foo();
foobar
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!
The function that we pass as an argument to another function, is called a Callback function. sayHello is a Callback function.
function sayHello() {
return function() {
console.log("Hello!");
}
}
A function that returns a function is called a Higher-Order Function.
const sayHello = function() {
return function() {
console.log("Hello!");
}
}
const myFunc = sayHello();
myFunc();
Hello!
Sources: http://tutorials.jenkov.com/java-functional-programming/index.html
https://developer.mozilla.org/en-US/docs/Glossary/First-class_Function