How to create nested functions in Javascript?
Nested functions in JavaScript.
Approach:
1. Write one function inside another function.
2. Make a call to the inner function in the return statement of the outer function.
3. Call it fun(a)(b) where a is parameter to outer and b is to the inner function.
4. Finally return the combined output from the nested function.
<!DOCTYPE HTML>
<html>
<head>
<title>
Nested functions in JavaScript.
</title>
</head>
<body id = "body" style = "text-align:center;">
<h1 style = "color:green;" >
GeeksforGeeks
</h1>
<p id = "GFG_UP" style =
"font-size: 15px; font-weight: bold;">
</p>
<button onclick = "GFG_Fun()">
click here
</button>
<p id = "GFG_DOWN"
style = "font-size: 24px;
font-weight: bold;
color: green;">
</p>
<script>
var up = document.getElementById('GFG_UP');
var down = document.getElementById('GFG_DOWN');
up.innerHTML =
"Click on the button to call nested function.";
function fun1(a) {
function fun2(b) {
return a + b;
}
return fun2;
}
function GFG_Fun() {
down.innerHTML =
fun1("A Online Computer Science Portal")
(" GeeksforGeeks");
}
</script>
</body>
</html>
Output
Before clicking on the button:

After clicking on the button:


After clicking on the button:
