JavaScript | Nested functions

Here the task is to create nested functions, JavaScript support nested functions. In the examples given below the output returning is combination of the output from the outer as well as inner function(nested function).
Approach:

  • Write one function inside another function.
  • Make a call to the inner function in the return statement of the outer function.
  • Call it fun(a)(b) where a is parameter to outer and b is to the inner function.
  • Finally return the combined output from the nested function.

Example 1: This example using the approach discussed above.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:



  • Before clicking on the button:
  • After clicking on the button:

Example 2: This example using the approach discussed above, but here the nested function is created differently than previous one.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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) {  
              fun = function fun2(b) { 
                return a + b;
              }
              return fun;
            }
            function GFG_Fun() {
                down.innerHTML = fun1("This is ")("GeeksforGeeks");
            
        </script>  
    </body>  
</html

chevron_right


Output:

  • Before clicking on the button:

  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.