JavaScript > Functions > Function Declarations and Expressions > Function expressions
Function Expressions in JavaScript
This code snippet demonstrates function expressions in JavaScript, showcasing their flexibility and use cases. It covers basic syntax, assignment to variables, and their application in creating anonymous functions.
Basic Function Expression
This example shows how to create a function expression. A function expression is a function that is created and assigned to a variable. The function can be anonymous (as shown here, without a name after the `function` keyword) or it can have a name. The function is then invoked by calling the variable like a normal function. The `add` variable now holds a reference to the function, allowing us to execute it.
// Assigning a function to a variable
const add = function(a, b) {
return a + b;
};
// Calling the function
console.log(add(5, 3)); // Output: 8
Anonymous Function Expression
An anonymous function is a function without a name. Here, we assign an anonymous function to the `multiply` variable. While the function itself doesn't have a name, it's still accessible through the variable it's assigned to.
// Anonymous function expression
const multiply = function(a, b) {
return a * b;
};
console.log(multiply(4, 6)); // Output: 24
Named Function Expression
A named function expression is a function expression where the function has a name. The name is only accessible within the function itself, commonly used for recursion. In this example, `fact` is used within the `factorial` function for the recursive call. Attempting to call `fact` outside of the `factorial` function will result in an error.
// Named function expression
const factorial = function fact(n) {
if (n <= 1) {
return 1;
} else {
return n * fact(n - 1); // Using the function name 'fact' for recursion
}
};
console.log(factorial(5)); // Output: 120
//console.log(fact(5)); // Throws an error: fact is not defined outside the function itself
Concepts Behind Function Expressions
Function expressions provide flexibility by allowing functions to be treated as values. This enables powerful features like passing functions as arguments to other functions (callbacks) and returning functions from other functions (closures). They're a fundamental building block for functional programming in JavaScript.
Real-Life Use Case: Event Handlers
A common use case is attaching event listeners to HTML elements. Here, an anonymous function expression is used as the callback function for the 'click' event of a button. When the button is clicked, the alert message will be displayed.
// Example with an event listener
document.getElementById('myButton').addEventListener('click', function() {
alert('Button clicked!');
});
Best Practices
Interview Tip
Be prepared to explain the difference between function declarations and function expressions. Function declarations are hoisted (can be called before they appear in the code), while function expressions are not. Also, understand the scoping differences with named function expressions.
When to Use Them
Use function expressions when you need to:
Memory Footprint
Function expressions consume memory like any other variable holding a function. They are created and stored in memory when the code is executed. The memory is released when the variable goes out of scope and the function is no longer referenced. Closures can potentially increase the memory footprint because they retain access to variables from their surrounding scope.
Alternatives
Pros
Cons
FAQ
-
What is the difference between a function declaration and a function expression?
A function declaration is defined using the `function` keyword followed by a function name. It is hoisted, meaning it can be called before it appears in the code. A function expression is a function that is assigned to a variable. It is not hoisted and must be defined before it is called. -
What is an anonymous function?
An anonymous function is a function without a name. In JavaScript, anonymous functions are often used as function expressions or as arguments to other functions (callbacks). -
What is a named function expression?
A named function expression is a function expression where the function has a name. The name is only accessible within the function itself, commonly used for recursion.