Q
What will be the output of the following code?
var a = 1; { var a = 2; } console.log(a);

Answer & Solution

Answer: Option B
Solution:
The variable a inside the block will override the outer variable a due to var's function scope.
Related Questions on Average

What will be the output of the following code?

let a = 1; let b = 2; let c = a + b; console.log(c);

A). 12

B). 12

C). 3

D). NaN

Can variables declared with 'var' be redeclared in the same scope?

A). Yes

B). No

C). Only inside a function

D). Only inside a loop

What is hoisting in JavaScript?

A). Function call before declaration

B). Variable declared at the top

C). Variable and function declaration are moved to the top

D). Variable scope

What is the difference between let and var?

A). let is function-scoped, var is block-scoped

B). let is block-scoped, var is function-scoped

C). Both are block-scoped

D). Both are function-scoped

Which of the following will cause an error?

A). let a = 10; a = 20;

B). const a = 10; a = 20;

C). var a = 10; a = 20;

D). None of the above

Which statement is true about variables in JavaScript?

A). They must be declared before use

B). They can only hold numbers

C). They are statically typed

D). They are strongly typed

Which of the following is NOT a valid variable name in JavaScript?

A). myVar

B). $myVar

C). _myVar

D). 123myVar

What will be the output of the following code?

var x = 5; var y = 6; var z = x + y;

A). 11

B). 11

C). 5

D). 6

Which keyword is used to declare a variable in JavaScript?

A). var

B). let

C). const

D). All of the above

Can you reassign a value to a variable declared with let?

A). Yes

B). No

C). Only in strict mode

D). Only inside a block