[Effective JavaScript notes] 9th: always declare local variables

Source: Internet
Author: User

If you forget to declare a variable as a local variable, the variable will be implicitly converted to a global variable

Function Swap (A,I,J) {

Temp=a[i];

A[I]=A[J];

A[j]=temp;

}

Although the program does not declare the TEMP variable using VAR, execution does not go wrong, and temp is added to the global usage domain and becomes a global variable.

Code that has been revised

Function Swap (A,I,J) {

var temp=a[i];

A[I]=A[J];

A[j]=temp;

}

Intentional creation of global variables is a bad style, and accidental creation is a disaster.

You can use the Lint tool to detect code. Using the tool can improve the quality of your JS code, it is worth spending time to spend a bit. Many small error problems can be avoided.

Tips

  • Always declare a new local variable with VAR
  • Consider using tools such as lint to help examine the variables of your code

[Effective JavaScript notes] 9th: always declare local variables

Related Article

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.