Take a look at the following code:
1 let x = 1;
2 function f1()
3 {
4 let x = 2;
5 console.log(x);
6 }
7 console.log(x);
Explain why line 4 and line 6 output different numbers. line 5 output has got access to the local variable x line 7 has got access to the global variable x
Take a look at the following code:
let x = 10
function f1()
{
console.log(x)
let y = 20
}
console.log(f1())
console.log(y)
What will be the output of this code. Explain your answer in 50 words or less. line 33 output: 10 line 34 output: undefined as y is local variable to the function f1, console.log outside the function doesnt have access to the variable
Take a look at the following code:
const x = 9;
function f1(val) {
val = val + 1;
return val;
}
f1(x);
console.log(x);
const y = { x: 9 };
function f2(val) {
val.x = val.x + 1;
return val;
}
f2(y);
console.log(y);
What will be the output of this code. Explain your answer in 50 words or less.
first console.log is going to be value 9 as const x passed to the function is not going to be changed second console.log is going to be { x: 10 } as value of the key x in the object can be changed using reference