Q
What will be the output of the following code?
let f = 10; if (true) { console.log(f); let f = 20; }

Answer & Solution

Answer: Option D
Solution:
let is hoisted but not initialized in the block scope, resulting in a ReferenceError when accessed before declaration.
Related Questions on Average

What will be the output of the following code?

let i = 50; { let i = 55; console.log(i); } console.log(i);

A). 50 50

B). 55 50

C). 50 55

D). ReferenceError

What will be the output of the following code?

{ console.log(b); let b = 20; }

A). 20

B). undefined

C). null

D). ReferenceError

What will be the output of the following code?

let c = 5; { let c = 10; console.log(c); }

A). 5

B). 10

C). ReferenceError

D). undefined

What will be the output of the following code?

console.log(a); let a = 10;

A). undefined

B). 10

C). null

D). ReferenceError

How can you avoid variable hoisting issues with let?

A). Declare variables at the bottom of the code

B). Use var instead

C). Declare variables at the top of their scope

D). Do not declare variables

What will be the output of the following code?

let h = 40; { console.log(h); h = 45; } console.log(h);

A). 40 45

B). 45 45

C). undefined undefined

D). ReferenceError 45

Which keyword should be used to declare variables that do not get re-assigned?

A). let

B). const

C). var

D). function

Which statement about let and const is correct?

A). Both are block scoped, but only let can be re-assigned

B). Both are block scoped, but only const can be re-assigned

C). Only const is block scoped

D). Both are not block scoped

Where should let variables be declared to avoid TDZ issues?

A). At the top of the global scope

B). At the bottom of their block scope

C). At the top of their block scope

D). Anywhere in the code

What is the scope of a variable declared with let inside a loop?

A). Global scope

B). Function scope

C). Block scope

D). Module scope