-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_nested_function.js
More file actions
27 lines (21 loc) · 1.11 KB
/
03_nested_function.js
File metadata and controls
27 lines (21 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Topic: Nested Function
// Q. What is nested function?
// -> A Nested Function (or inner function) is simply a function that is defined inside the body of another function (the outer function).
// Q. Why use nested function?
// -> Nested functions can be useful for creating private variables and encapsulating functionality. By defining functions inside other functions, you can limit their accessibility and keep your code organized.
// Note:
// -> When a function is defined inside another function, it becomes a local function, meaning it is only accessible within the scope of the outer function.
// -> A nested function retains access to the outer function's scope even after the outer function has finished executing. This ability to remember and access its birth environment is what is called a Closure.
// Example:
function outerFunction() {
// Outer function scope
function innerFunction() {
// Inner function scope
console.log("This is the inner function.");
}
console.log("This is the outer function.");
// Call the inner function
innerFunction();
}
// Call the outer function
outerFunction();