Preface
Supplement on June 9, 2021:
Thanks to the reader blackr1234 for leaving a comment. This article was published in November 2018, and the output results of the code below are probably based on Node.js v8.17.0, so the output of some situations may be different from now. For example, accessing the let variable before declaration, the result at that time was: ReferenceError: a is not defined
, and the result using Node.js v14 now is: ReferenceError: Cannot access 'a' before initialization
.
Recently, I have been busy with some teaching-related things, and after preparing some materials, I taught my students about hoisting in JavaScript, which is the concept of “lifting”. For example, the following code:
console.log(a)
var a = 10
will output undefined
instead of ReferenceError: a is not defined
. This phenomenon is called Hoisting, and the declaration of the variable is “lifted” to the top.
If you only want to understand the basics of hoisting, it’s almost like this, but later I also taught some knowledge related to let
and const
. However, the day before I just finished teaching, the next day I immediately saw a related technical article and found that I had taught it wrong. Therefore, I spent some time planning to understand hoisting well.
Many things may not seem like much before you delve into them. You will find that you still have a lot of concepts that you don’t understand when you really jump in and look deeply.
Many people know hoisting, but the degree of understanding is different. I have listed 10 items. If you don’t know any of them, congratulations, this article should bring you some gains.
- You know what hoisting is
- You know that hoisting only lifts declarations, not assignments
- You know the priority of hoisting when function declarations, function parameters, and general variable declarations appear at the same time
- You know that let and const do not have hoisting
- You know that the fourth point is wrong, in fact, there is hoisting, but the expression is different
- You know that there is a concept called TDZ (Temporal Dead Zone) related to the fifth point
- You have read the ES3 specification and know how it is described inside
- You have read the ES6 specification and know how it is described inside
- You know the principle behind hoisting
- You have seen the code compiled by V8
You may ask, “Why do I need to know so deeply? What’s the use?” In fact, I also think that for hoisting, it is enough to know the basics. As long as you declare variables properly, even if you don’t know those, it will not have much impact on daily life or work.
But if you, like me, want to put “proficient in JavaScript” on your resume one day, you cannot escape these things. At the same time, if you are more familiar with these underlying details, you will encounter fewer problems, and you will also understand why hoisting appears. When you want to go further and climb higher on the technical road, I think these details are very important.
Next, let’s take a look at hoisting step by step!
Read More