Daily JavaScript Quiz - 2024-03-17

  • Home /
  • Daily JavaScript Quiz - 2024-03-17

What will be the output of the above code? - 2024-03-17

function randomOutput() {
  let x = 10;
  let y = 20;

  function innerFunction() {
    x = 30;
    let z = 40;
    console.log(`Inside innerFunction: x = ${x}, y = ${y}, z = ${z}`);
  }

  innerFunction();
  console.log(`Outside innerFunction: x = ${x}, y = ${y}`);
}

randomOutput();

Inside innerFunction: x = 30, y = 20, z = 40 Outside innerFunction: x = 30, y = 20

Inside innerFunction: x = 10, y = 20, z = 40 Outside innerFunction: x = 30, y = 20

Inside innerFunction: x = 30, y = 20, z = 40 Outside innerFunction: x = 10, y = 20

Inside innerFunction: x = 30, y = undefined, z = 40 Outside innerFunction: x = 30, y = 20

The answer is Option 1

  • Inside randomOutput:

    • x and y are defined with values 10 and 20 respectively.

    • innerFunction modifies x to 30 and logs x = 30, y = 20, and z = 40.

    • After calling innerFunction, it logs x = 30 and y = 20.

So the output will be:

Inside innerFunction: x = 30, y = 20, z = 40

Outside innerFunction: x = 30, y = 20

Javascript Quizzes