Multiple choice

What will be the output of the above code?

fun3();
function fun1(i) {
  function fun2(j) { return i * j; }
  return fun2;
}
function fun3() {
  r = fun1(5)(5);
  alert(r);
}

  1. 5

  2. 15

  3. 10

  4. 25

  5. Compilation Error

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Functions are another type of variable in JavaScript. Creating a function within another function changes the scope of the function in the same way it would change the scope of a variable.The functions defined within another function won't be accessible outside the function unless they have been attached to an object that is accessible outside the function. In the above code, we are calling the function named 'fun3()' , which in turn is calling another function named 'fun1()' with two arguments (5) and (5). The fun1() will copy the value of the first 5 to i and the second value will be copied to j. The result of fun2() will be sent to 'fun1()'. fun1() will return the result (25) to fun3(). The result will be copied to the variable 'r' and printed using an alert box.