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

Answer & Solution

Answer: Option A
Solution:
The outer x variable remains unchanged because the inner x is scoped to the block. Thus, the output is 10.
Related Questions on Average

Which keyword is used to declare a read-only named constant?

A). let

B). var

C). const

D). static

Are variables declared with let and const accessible before they are declared?

A). Yes

B). No

C). Only in strict mode

D). Only if they are numbers

Can a let variable be redeclared in the same scope?

A). Yes

B). No

C). Only if it is a string

D). Only if it is a number

What is the value of a const variable once it is assigned?

A). Can be changed later

B). Cannot be changed later

C). Depends on the data type

D). Undefined initially

What will the following code output: { let y = 5; { let y = 10; console.log(y); } }?

A). 5

B). 10

C). undefined

D). ReferenceError

What is the primary difference between var and let?

A). Scope

B). Initialization

C). Data type

D). Assignment

Can const be used for objects and arrays?

A). No

B). Yes

C). Only for arrays

D). Only for objects

What happens if you try to access a let variable before its declaration?

A). Returns undefined

B). Throws a ReferenceError

C). Returns null

D). Returns an empty string

Which keyword should be used for a variable that might be reassigned?

A). const

B). let

C). var

D). function

Which keyword allows block-level scope in ES6?

A). var

B). let

C). function

D). var and function