Introduction
Namaste, In this blog I will discuss 7 code scenarios on Javascript 'let' keyword.
Identify the output of the below program
console.log ("myval2", myval2); let myval2=101;
a.101
b.null
c.error
d.undefined
e.no output
Answer:c
Reason:The let variables are not hoisted
Identify the output of the below program
for (let a=10;a<11;) { a++; } console.log(a);
a.10
b.11
c.undefined
d.error
e.no output
Answer:d
Reason:The let keyword has block scope
Identify the output of the below program
let i=11; if ( i > 9){ let c = 13; } console.log("c:", c);
a.11
b.13
c.no output
d.undefined
e.error
Answer:e
Reason:The let keyword has block scope
Identify the output of the below program
function fx() { let myval3=200; console.log ("myval4", myval4); let myval4=201; } fx();
a.200
b.201
c.error
d.undefined
e.no output
Answer:c
Reason:The let variables are not hoisted
Identify the output of the below program
let myval5=30; let myval5=40; console.log(myval5)
a.30
b.40
c.error
d.undefined
e.null
Answer:c
Reason:The let variable cannot be re-declared
Identify the output of the below program
// Block scope inside function function fx2() { try { let d=20; }catch(e) { console.log("error"); } console.log(d); } fx2();
a. 20
b.error
c.no output
d.11
e.undefined
Answer:b
Reason:The let variable has block scope
Identify the output of the below program
let a=10; const a=20; console.log(a);
a.10
b.20
c.no-output
d.error
e.undefined
Answer:d
Reason: A variable once declared using let cannot be re-declared.