console.log(greetings); var greetings = "hello world";
Output: ------- undefined
However, if you run the same code by replacing the var keyword with let or const keyword, it will produce ReferenceError.
console.log(greetings); let greetings = "hello world";
Output: ------- ReferenceError: greetings is not defined
Why? Why hoisting behaves differently when variable declared with let or const keyword?
In a nutshell, a variable declared with let or const keyword do hoist but don't get initialized and hence produced ReferenceError. To avoid this temporal dead zone behavior, you should always declare and initialize variables at the top of their scope when declared with let or const keyword.
You are welcome to write comments, suggestions, corrections, or any queries related to the article. Your comments may take some time to be appeared. Please be aware that any irrelevant comments will be deleted. Thanks for your understanding, and your respectful & relevant comments!