Predict the output of the following javascript code
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();
Quiz Explanation
Inside
randomOutput
:x
andy
are defined with values10
and20
respectively.innerFunction
modifiesx
to30
and logsx = 30
,y = 20
, andz = 40
.After calling
innerFunction
, it logsx = 30
andy = 20
.
So the output will be:
Inside innerFunction: x = 30, y = 20, z = 40
Outside innerFunction: x = 30, y = 20