Small mistakes in JavaScript-------Common mistakes two: The life-cycle myths of traditional programming languages

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.