Facebook Pixel
Common Hoisting Interview Questions in JavaScript

What does console.log(a) print if var a = 5 is below it?

The answer

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.

More FAQs in Common Hoisting Interview Questions in JavaScript

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.

The last one wins. Both are hoisted with their full bodies, and the later declaration overwrites the earlier one in memory. So calls after both declarations always invoke the last one.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

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.
Please Login.
Please Login.
Please Login.