-
Consider the following code snippet
function hypotenuse(p, q)
{
function square(n)
{
return n*n;
}
return Math.sqrt(square(p) + square(q));
}
What does the above code result?
-
- Square of sum of p and q
- Sum of p and q square
- Sum of square of p and q
- All of above
- None of these
Correct Option: C
The above code snippet contains nested function in which the function hypotenuse(p,q) has another function inside its scope, function square(n). The interesting thing about nested functions is their variable scoping rules. They can access the parameters and variables of the function (or functions) they are nested within.