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
- Functions can be assigned to variables or passed as arguments.
- They are not hoisted, meaning they can’t be used before declaration.
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
- Shorter syntax: No need for
function
keyword. - Implicit return: If there’s a single expression, the return keyword can be omitted.
- Lexical
this
binding: Useful in event handlers and callbacks.
When to Use Arrow Functions
- Great for short functions and callbacks.
- Avoid using them when you need a function with its own
this
(e.g., object methods).
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
- Enables functional programming techniques.
- Reduces code duplication.
- Improves readability and maintainability.
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
Function Expressions in JavaScript Example of a Function Expression Benefits of Function Expressions Arrow Functions in JavaScript Example of an Arrow Function Key Features of Arrow Functions When to Use Arrow Functions Higher-Order Functions in JavaScript Example: Using map (a Higher-Order Function) Example: Creating a Function that Returns a Function Benefits of Higher-Order Functions Conclusion