Functions in JavaScript

Date : 02 Jul 2025
Author : You
Tags :

Function Expressions in JavaScript

A function expression is a way to define functions as part of an expression, often assigned to a variable.

Example of a Function Expression

const add = function(a, b) {
  return a + b;
};
console.log(add(5, 3)); // Output: 8

Benefits of Function Expressions

Arrow Functions in JavaScript

Arrow functions provide a concise way to define functions and automatically bind this from the surrounding scope.

Example of an Arrow Function

const multiply = (a, b) => a * b;
console.log(multiply(4, 2)); // Output: 8

Key Features of Arrow Functions

When to Use Arrow Functions

Higher-Order Functions in JavaScript

A higher-order function is a function that takes another function as an argument or returns a function.

Example: Using map (a Higher-Order Function)

const numbers = [1, 2, 3, 4];
const squared = numbers.map(num => num * num);
console.log(squared); // Output: [1, 4, 9, 16]

Example: Creating a Function that Returns a Function

const createMultiplier = factor => number => number * factor;
const double = createMultiplier(2);
console.log(double(5)); // Output: 10

Benefits of Higher-Order Functions

Conclusion

Understanding function expressions, arrow functions, and higher-order functions is essential for writing clean and efficient JavaScript. Function expressions provide flexibility, arrow functions offer concise syntax, and higher-order functions enhance modularity in code.

Table of Contents