Function Expressions
EasyIn plain terms
A function can be stored in a variable. const fn = function() {} is a function expression. Unlike declarations, expressions are not hoisted. Anonymous functions have no name.
What you need to know
- •Stored in variable
- •Not hoisted
- •Can be anonymous or named
Try it yourself
Copy the code below and run it in your browser console or a code editor:
const sayHi = function() {
console.log('Hi');
};
sayHi();
const add = function(a, b) {
return a + b;
};
// Pass as callback
setTimeout(function() {
console.log('Later');
}, 1000);
// Named expression (for recursion)
const factorial = function fact(n) {
return n <= 1 ? 1 : n * fact(n - 1);
};Learn more
Dive deeper with these trusted resources: