JavaScript - Nested Functions

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. Still there is a restriction that function definitions may not appear within loops or conditionals. These restrictions on function definitions apply only to function declarations with the function statement.

  •       
                        <html>
    <head>
    <script type = "text/javascript">
        
        function hypotenuse(a, b) {
        function square(x) { return x*x; }
        return Math.sqrt(square(a) + square(b));
        }
        function secondFunction() {
        var result;
        result = hypotenuse(1,2);
        document.write ( result );
        }
    </script>
    </head>
    
    <body>
    <p>Click the following button to call the function</p>
    
    <form>
    <input type = "button" onclick = "secondFunction()" value = "Call Function">
    </form>
    
    <p>Use different parameters inside the function and then try...</p>
    </body>
    </html>
                        

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1