Q
Which of the following correctly describes variable hoisting with 'var'?

Answer & Solution

Answer: Option B
Solution:
In JavaScript, 'var' declarations are hoisted to the top of their scope, but the initialization stays in place. This can lead to 'undefined' values before the actual assignment.
Related Questions on Average

What will happen if you try to use a variable before declaring it with 'let'?

A). It will return undefined

B). It will return null

C). It will throw a ReferenceError

D). It will return NaN

What happens when you redeclare a variable with 'var' inside a function?

A). The variable is overwritten

B). The variable declaration is ignored

C). It throws an error

D). The function's scope is reset

Can you redeclare a 'var' variable in a different scope without error?

A). Yes

B). No

C). Only in strict mode

D). Only if the variable is not initialized

Why is it recommended to use 'let' and 'const' over 'var' in modern JavaScript?

A). 'let' and 'const' are function-scoped

B). 'let' and 'const' prevent variable hoisting

C). 'let' and 'const' are block-scoped, reducing potential errors

D). 'let' and 'const' are faster

Which of the following statements about 'let' and 'const' is false?

A). Both 'let' and 'const' are block-scoped

B). Both 'let' and 'const' cannot be redeclared in the same scope

C). 'let' can be reassigned, 'const' cannot

D). Both 'let' and 'const' can be redeclared in different scopes

How does the scope of 'var' differ from 'let' and 'const' inside a function?

A). 'var' is block-scoped, 'let' and 'const' are function-scoped

B). 'var', 'let', and 'const' are all block-scoped

C). 'var' is function-scoped, 'let' and 'const' are block-scoped

D). 'var' and 'let' are block-scoped, 'const' is function-scoped

What error is thrown when attempting to redeclare a 'const' variable?

A). TypeError

B). ReferenceError

C). SyntaxError

D). RangeError

Which of the following statements is true regarding redeclaring variables in JavaScript?

A). You can redeclare 'var' variables within the same scope without errors

B). You can redeclare 'let' variables within the same scope without errors

C). You can redeclare 'const' variables within the same scope without errors

D). None of the above

Which of the following is a characteristic of 'const' variables?

A). They can be redeclared

B). They can be reassigned

C). They cannot be redeclared but can be reassigned

D). They cannot be redeclared or reassigned

What will happen if you declare a 'let' variable inside a block and try to access it outside the block?

A). It will return undefined

B). It will throw a ReferenceError

C). It will return null

D). It will be accessible outside the block