What are JavaScript Nested Functions?

JavascriptWeb DevelopmentFront End Technology

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. 


You can try to run the following code to learn how to implement JavaScript Nested Functions −

         function hypotenuse(a, b) {
            function square(x) { return x*x; }
            return Math.sqrt(square(a) + square(b));
         function secondFunction(){
            var result;
            result = hypotenuse(3,4);
            document.write ( result );
      <p>Click the following button to call the function</p>
         <input type = "button" onclick = "secondFunction()" value = "Find Hypotenuse">
Published on 16-Feb-2018 16:45:27