Functions are another type of variable in JavaScript (with some nuances of course). Creating a function within another function changes the scope of the function in the same way it would change the scope of a variable. This is especially important for use with closures to reduce total global namespace pollution.

The functions defined within another function won't be accessible outside the function unless they have been attached to an object that is accessible outside the function:

function foo(doBar)
{
  function bar()
  {
    console.log( 'bar' );
  }

  function baz()
  {
    console.log( 'baz' );
  }

  window.baz = baz;
  if ( doBar ) bar();
}

In this example, the baz function will be available for use after the foo function has been run, as it's overridden window.baz. The bar function will not be available to any context other than scopes contained within the foo function.

as a different example:

function Fizz(qux)
{
  this.buzz = function(){
    console.log( qux );
  };
}

The Fizz function is designed as a constructor so that, when run, it assigns a buzz function to the newly created object. That is, you'd use it like this:

const obj = new Fizz();
obj.buzz();

or more concisely (if you don't need to keep the object after calling buzz):

new Fizz().buzz();
Answer from zzzzBov on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-nested-functions
JavaScript Nested functions - GeeksforGeeks
A nested function is a function defined inside another function in JavaScript.
Published: July 12, 2025
Top answer
1 of 8
166

Functions are another type of variable in JavaScript (with some nuances of course). Creating a function within another function changes the scope of the function in the same way it would change the scope of a variable. This is especially important for use with closures to reduce total global namespace pollution.

The functions defined within another function won't be accessible outside the function unless they have been attached to an object that is accessible outside the function:

function foo(doBar)
{
  function bar()
  {
    console.log( 'bar' );
  }

  function baz()
  {
    console.log( 'baz' );
  }

  window.baz = baz;
  if ( doBar ) bar();
}

In this example, the baz function will be available for use after the foo function has been run, as it's overridden window.baz. The bar function will not be available to any context other than scopes contained within the foo function.

as a different example:

function Fizz(qux)
{
  this.buzz = function(){
    console.log( qux );
  };
}

The Fizz function is designed as a constructor so that, when run, it assigns a buzz function to the newly created object. That is, you'd use it like this:

const obj = new Fizz();
obj.buzz();

or more concisely (if you don't need to keep the object after calling buzz):

new Fizz().buzz();
2 of 8
43

It is called closure.

Basically, the function defined within other function is accessible only within this function. But may be passed as a result and then this result may be called.

It is a very powerful feature. You can see more explanation here:

javascript_closures_for_dummies.html mirror on Archive.org

🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_nested_functions.htm
JavaScript - Nested Functions
Prior to JavaScript 1.2, function definition was allowed only in top level global code, but JavaScript 1.2 allows function definitions to be nested within other functions as well.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › javascript tutorial › javascript nested functions
Javascript Nested Functions | 4 Examples of Javascript Nested Functions
June 19, 2023 - Functions within another function are called “Nested function.” A function can have one or more inner functions. These nested functions (inner functions) are under the outer functions’ scope.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Variable scope, closure
A function is called “nested” when it is created inside another function. It is easily possible to do this with JavaScript.
🌐
Codingzap
codingzap.com › home › blog – programming & coding articles › javascript nested functions: syntax, examples & best practices
JavaScript Nested Functions: Syntax, Examples & Best Practices - Codingzap
March 16, 2026 - The above discussion shows that every variable declared in the JavaScript Program can be accessed from the Child Function Scope. So, it is proven that the Global, Outer, and Inner function is accessible from the Inner Scope of the Nested Functions.
Find elsewhere
🌐
Tektutorialshub
tektutorialshub.com › home › javascript › nested functions in javascript
Nested Functions in JavaScript - Tektutorialshub
October 22, 2022 - The increment function can access it because it is nested inside the makeCounter function. In this line of code, we invoke the makeCounter, which initializes the count to 0 and returns the increment function. ... Usually, we expect JavaScript to clean up the memory when the function returns.
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › javascript nested functions: inner function and closure examples
JavaScript nested functions: function inside function and closures
June 16, 2026 - A nested function is simply a function declared inside another function—sometimes called an inner function. It can read and close over variables from the outer function’s lexical environment, which is how JavaScript builds private helpers, ...
🌐
Microsoft MakeCode
arcade.makecode.com › courses › csintro4 › appendix › nested-functions
Advanced Topics: Nested Functions
Instead of a single function to ... better manage this behavior, JavaScript allows for nested functions: functions that exist only within another function....
🌐
Delft Stack
delftstack.com › home › howto › javascript › function within a function javascript
Nested Functions in JavaScript | Delft Stack
March 4, 2025 - Nested functions are simply functions defined within another function. This structure allows the inner function to access variables and parameters of the outer function, creating a closure.
🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-tutorial-part-25-nested-functions-in-javascript-df15cc44de05
JavaScript Tutorial- Nested Functions in JavaScript | by Coding Adventure with Emma | JavaScript in Plain English
April 26, 2025 - In JavaScript, a nested function refers to a function defined inside another function. The inner function has access to the variables, parameters, and scope of the outer function, creating a scope chain.
🌐
LinkedIn
linkedin.com › pulse › mastering-javascript-nested-functions-closures-javascript-code-yrvuc
Mastering JavaScript: Nested Functions and Closures
November 21, 2023 - Download JavaScript PDF Guides ... and Encapsulation In JavaScript, nested functions refer to the practice of defining a function inside another function....
🌐
DEV Community
dev.to › rajajaganathan › why-nested-functions-in-javascript-3ngp
Why nested functions in JavaScript - DEV Community
May 26, 2020 - Often many people confused about nested function in JavaScript, why the nested function is useful, an... Tagged with javascript, node, typescript, webdev.
🌐
Suneet Agrawal
agrawalsuneet.github.io › blogs › nested-function-in-javascript
Nested Function in JavaScript · Suneet Agrawal
August 18, 2023 - JavaScript is a versatile and widely used programming language that empowers developers to create interactive and dynamic web applications. One of its powerful features is the ability to define functions within functions, known as nested functions.
🌐
Scientech Easy
scientecheasy.com › home › blog › nested functions in javascript
Nested Functions in JavaScript - Scientech Easy
March 10, 2026 - Similar to nested if statements, when we define a function inside another function, it is called nested functions in JavaScript.
🌐
GeeksforGeeks
geeksforgeeks.org › videos › javascript-nested-functions
JavaScript Nested functions - GeeksforGeeks | Videos
Understanding closure, the mechanism by which nested functions retain access to their outer function's variables even after the outer function has finished executing, is crucial for mastering nested functions. We'll discuss closure and the scope chain in JavaScript in detail.
Published: June 5, 2024
Views: 2K