A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-nested-functions/ below:

JavaScript Nested functions - GeeksforGeeks

JavaScript Nested functions

Last Updated : 12 Jul, 2025

A nested function (also known as an inner function) is a function that is declared within another function (known as the outer function). The inner function has access to the variables of its outer function, forming a lexical scope chain.

JavaScript
function outer() {
    console.log('This is the outer function')
    function inner() {
        console.log("This is the inner function.")
    }
    inner();
}
outer(); 

Output
This is the outer function
This is the inner function.

In this code

How nested functions work in JavaScript Inner Function Accessing Outer Variables

Inner functions can access variables from the outer function, which can be useful for manipulating data within the scope.

JavaScript
function outer(a, b) {
    function inner() {
        return a + b;
    }
    console.log(inner());
}
outer(2, 3)

In this code

Returning an Inner Function (Closure)

Nested functions can return inner functions, creating closures that "remember" the outer function’s scope.

JavaScript
function outer(x) {
    return function inner(y) {
        return x + y;
    };
}

const addFive = outer(5);
console.log(addFive(3)); 

In this code

Nested Functions with Multiple Arguments

You can create more complex nested functions that accept multiple arguments.

JavaScript
function outer(x, y) {
    function inner(a, b) {
        return a * b + x + y;
    }
    return inner(3, 4);
}

console.log(outer(2, 5));

In this code

Nested Functions for Encapsulation

Nested functions can also be used for encapsulating logic and keeping variables private within the outer function's scope.

JavaScript
function counter() {
    let count = 0;
    function increment() {
        count += 1;
        return count;
    }
    return increment;
}

const myCounter = counter();
console.log(myCounter());
console.log(myCounter());

In this code

Greeting according to time of day

You can use nested functions to create a greeting message based on the time of day. The outer function calls the inner function to determine the part of the day (morning, afternoon, evening) and then returns the appropriate greeting.

JavaScript
function greet(name) {
    const hours = new Date().getHours();
    const timeOfDay = hours < 12 ? "Good morning" : hours < 18 ? 
        "Good afternoon" : "Good evening";
    console.log(`${timeOfDay}, ${name}!`);
}

greet("Pranjal");
Greeting according to time of day Benefits of Using Nested Functions
  1. Encapsulation: Inner functions help in structuring the code by keeping helper functions private and preventing unwanted access.
  2. Code Organization: They help break down large functions into smaller, manageable units.
  3. Access to Outer Variables: Inner functions can access the variables and parameters of their outer function due to lexical scoping.
  4. Avoiding Global Pollution: Since inner functions are not accessible outside their outer function, they do not interfere with the global scope.
Limitations of Nested Functions Conclusion

Nested functions in JavaScript are a great way to structure your code, encapsulate logic, and create powerful closures. By understanding how they work and using them effectively, you can write cleaner, more modular, and maintainable code.


JavaScript Nested functions


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4