First-class Functions
An important thing to remember about functions in JavaScript is: functions are values.
This means that functions are treated as first-class citizens, just like numbers and objects. They have all the properties we expect of other objects. Check out this medium blog post for a more in-depth explanation.
Let's look at this example:
// A named functionfunction triple (x) {return x * 3}// An anonymous functionfunction (x) {return x * 3}// An anonymous function assigned to a variableconst triple = function (x) {return x * 3}// An anonymous fat arrow function assigned to a variableconst triple = (x) => {return x * 3}
Because the last example uses a variable, you can do some cool things like this:
const engorgio = tripleconst value = engorgio(30) // value is 90
Both triple
and engorgio
refer to the same function. But the fun does not stop there! We can also pass functions into other functions as arguments.
A good example of this is the filter function, probably the most basic and useful higher order function. It's a function that operates on an array, and accepts another function as an argument that it uses to return a new filtered version of the array.
// Original arrayconst names = ['Alice', 'Bob', 'Charlie', 'David', 'Eva'];// Use filter to get names with length greater than 4const longNames = names.filter(function(name) {return name.length > 4;});console.log(longNames); // Output: ['Alice', 'Charlie']
So, to recap:
- We can store functions in variables
- We can store functions as properties of other objects
- Functions can have properties
- We can pass functions as arguments into other functions