Table of contents | |
Introduction | |
What are Function Expressions? | |
Function Expressions vs. Function Declarations | |
Examples and Code Explanations | |
Sample Problems with Solutions | |
Conclusion |
JavaScript is a versatile programming language that allows you to create interactive and dynamic web pages. One of the key features of JavaScript is its ability to work with functions. In this article, we will explore an important aspect of functions in JavaScript called function expressions. We will cover what function expressions are, how they differ from function declarations, and provide multiple examples and code explanations to help you grasp the concept easily.
Function expressions in JavaScript are a way to define functions as values and assign them to variables. In other words, instead of using the traditional function declaration syntax, you can create a function on the fly and assign it to a variable. This flexibility opens up new possibilities for working with functions in JavaScript.
Function expressions and function declarations may appear similar at first, but they have a few key differences:
Example 1: Basic Function Expression
// Anonymous function expression
let greet = function() {
console.log("Hello, world!");
};
greet(); // Output: Hello, world!
In this example, we define an anonymous function expression and assign it to the greet variable. The function does not have a name and can be called using the variable name followed by parentheses.
Example 2: Function Expression as an Argument
function calculateSquare(num, callback) {
return callback(num * num);
}
let result = calculateSquare(5, function(square) {
return square;
});
console.log(result); // Output: 25
Here, we have a function calculateSquare that takes a number and a callback function as arguments. The callback function is a function expression passed as an argument to calculateSquare. Inside calculateSquare, the callback function is called with the squared value of the input number. In this case, the callback function simply returns the square value, which is then assigned to the result variable.
Example 3: Immediately Invoked Function Expression (IIFE)
(function() {
console.log("This is an IIFE.");
})();
// Output: This is an IIFE.
An IIFE is a function expression that is immediately invoked. It is wrapped in parentheses and followed by an additional set of parentheses. This pattern is useful when you want to create a function that runs immediately without being assigned to a variable.
Problem 1: Write a function expression to calculate the factorial of a given number.
let factorial = function(num) {
if (num === 0 || num === 1) {
return 1;
} else {
return num * factorial(num - 1);
}
};
console.log(factorial(5)); // Output: 120
Problem 2: Use a function expression to find the average of an array of numbers.
let average = function(numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum / numbers.length;
};
let numbers = [1, 2, 3, 4, 5];
console.log(average(numbers)); // Output: 3
Function expressions in JavaScript provide a flexible way to work with functions. They allow you to assign functions to variables, use them as arguments, and create immediately invoked functions. By understanding the concepts and examples provided in this article, you should now have a solid foundation for using function expressions in your JavaScript code.
51 videos|28 docs|12 tests
|
|
Explore Courses for Software Development exam
|