Small mistakes in JavaScript-------Common mistakes two: The life-cycle myths of traditional programming languages
Another easy mistake is to take the thinking of other programming languages and think that in JS there is also a life cycle. Take a look at the following code:
for (var i = 0; i < i++) {
/* ... */
}
Console.log (i);
If you think you'll be sure to report a undefined error when you run Console.log (), you're wrong. I will tell you that it will return 10.
Of course, in many other languages, encountering such a code will definitely be an error. Because I clearly have transcended its life cycle. The variable defined in for is at the end of the loop, and its life is over. But in JS, I's life will continue. This phenomenon is called variable hoisting.
And if we want to implement a variable that has a life cycle in a particular logical module like any other language, you can use the Let keyword.
Small mistakes in JavaScript-------Common mistakes two: The life-cycle myths of traditional programming languages