Q
What will be the output of the following code: var x = 5; var x = 10; console.log(x);

Answer & Solution

Answer: Option B
Solution:
The output will be 10 because re-declaration of variables using 'var' is allowed and the latest assignment is considered.
Related Questions on Average

What will be the output of the following code: const d = 90; d = 100; console.log(d);

A). 90

B). 100

C). Error

D). undefined

How do 'var' declarations handle hoisting?

A). They are not hoisted

B). Only the assignment is hoisted

C). Only the declaration is hoisted

D). Both declaration and assignment are hoisted

What will be the output of the following code: let y = 10; let y = 20; console.log(y);

A). 10

B). 20

C). Error

D). undefined

Can you re-declare a variable using 'const' in the same scope?

A). Yes

B). No

C). Only in functions

D). Only in loops

What will be the output of the following code: var e = 110; if (true) { var e = 120; } console.log(e);

A). 110

B). 120

C). Error

D). undefined

Which keyword is used for declaring variables that should not change?

A). var

B). let

C). const

D). all of the above

Which of the following statements is true about 'const'?

A). It allows re-declaration

B). It allows reassignment

C). It is block-scoped

D). It is function-scoped

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

A). Global

B). Function

C). Loop block

D). Entire script

What will be the output of the following code: if (true) { let b = 60; } console.log(b);

A). 60

B). undefined

C). Error

D). null

What is the default scope of variables declared with the 'var' keyword in JavaScript?

A). Block scope

B). Function scope

C). Module scope

D). Block and function scope