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