Introduction
Namaste, In this blog I will discuss 7 code scenarios on Javascript fundamentals.
Identify the output of the below program
console.log("value",i); let i=10;
a.10
b.undefined
c.error
d. NaN
e.0
Answer: c
Reason: Since the variable 'i' is not hoisted, the interpreter yields an error.
Identify the output of the below program
let i=10; i=40; let i=50; console.log("value", i);
a. error
b.50
c.40
d. 10
e.undefined
Answer: a
Reason: Re-declaration using let is invalid.
Identify the output of the below program
let i=10; if (i>3) { let j=20; } console.log("value", j);
a. 20
b.10
c.undefined
d. NaN
e.error
Answer: e
Reason: Since let has a block scope the variable 'j' is not accessible outside the block.
Identify the output of the below program
function fx() { let i=10; function fx2() { console.log("value", i); } fx2(); } fx();
a.10
b.error
c.undefined
d. NaN
e.0
Answer: a
Reason: In the context of function fx2, the variable is global and hence accessible.
Identify the output of the below program
function fx() { function fx2() { console.log("value", i); } fx2(); let i=10; } fx();
a.undefined
b.NaN
c.error
d. 10
e.0
Answer: c
Reason: Since variable 'i' is not hoisted interpreter yields an error.
Identify the output of the below program
try { let i=10; }catch (e) { console.log(e); } console.log("value", i);
a.error
b.10
c.undefined
d. 0
e.null
Answer: a
Reason: Since variable 'i' has a block scope it is not accessible outside the block.
Identify the output of the below program
function fx() { let i=10; if (i>3) { let j=20; } console.log("value", j); } fx();
a.10
b.20
c.NaN
d. undefined
e.error
Answer: e
Reason: The variable 'j' has a block scope hence not accessible outside 'if' condition.