Facebook Pixel

How do you approach hoisting questions in an interview?

Walk through the memory phase first: what is hoisted and to what value (undefined for var, full body for function declarations, TDZ for let/const). Then walk the execution phase line-by-line to determine each output.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Common Hoisting Interview Questions in JavaScript

undefined. The var declaration is hoisted and initialized to undefined. The assignment a = 5 runs only when the line is reached, so the earlier log sees undefined.

TypeError: x is not a function. The var variable is hoisted with undefined, so calling it as a function fails. Only function declarations are hoisted with their body.

No. typeof on a let or const variable in the TDZ throws ReferenceError. typeof on a var that has not been assigned returns the string 'undefined' without throwing.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.