Blog / April 27, 2023 / 3 mins read / By Suneet Agrawal

Function Expression : JavaScript

JavaScript is a dynamic and versatile programming language that allows developers to create complex and dynamic applications. One of the key features of JavaScript is its ability to use functions as first-class citizens. This means that functions can be assigned to variables, passed as arguments to other functions, and even returned as values from functions. In this article, we’ll take a closer look at function expressions in JavaScript and explore how they can be used in your code.

Function Expressions

A function expression is a type of function that is defined as part of an expression. In other words, it’s a function that is defined on the fly, rather than being defined in advance as a named function. Here’s an example of a function expression:

let multiply = function(a, b) {
  return a * b;
}

In this example, we’ve created a function expression that takes two arguments, a and b, and returns their product. We’ve assigned this function expression to a variable called multiply. This means that we can call the multiply function just like we would any other function:

let result = multiply(5, 10);
console.log(result); // Output: 50

As you can see, we’ve passed the values 5 and 10 as arguments to the multiply function, and it has returned their product, 50.

Anonymous Function

One of the advantages of using function expressions is that they can be anonymous. This means that you don’t have to give them a name, which can be useful in certain situations. For example, if you want to create a function that you’ll only use once, you can define it as an anonymous function expression:

let result = (function(a, b) {
  return a * b;
})(5, 10);

console.log(result); // Output: 50

In this example, we’ve defined an anonymous function expression that takes two arguments, a and b, and returns their product. We’ve then immediately called this function expression with the values 5 and 10. The result of the function is assigned to a variable called result, which is then logged to the console.

Function as Arguments

Another advantage of using function expressions is that they can be used as arguments to other functions. For example, the Array.prototype.map() method takes a function as an argument and applies it to each element in an array. Here’s an example of how you can use a function expression with Array.prototype.map():

let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(function(number) {
  return number * 2;
});

console.log(doubled); // Output: [2, 4, 6, 8, 10]

In this example, we’ve defined a function expression that takes a single argument, number, and returns its product by 2. We’ve then passed this function expression as an argument to the Array.prototype.map() method, which applies the function to each element in the numbers array and returns a new array with the results.

Conclusion

Function expressions are a powerful tool in JavaScript that allows you to define functions on the fly and use them as first-class citizens. They can be assigned to variables, passed as arguments to other functions, and even returned as values from functions.

Comments