Home > Article > Web Front-end > Basic specifications of JavaScript
As everyone knows, each programming language has its own coding standards. The following are the basic coding standards for JavaScript that I have compiled.
1. Do not declare multiple variables on the same line
var a = null;var b = 0;var c = undefined;123
2. Please use ===/!== to compare true/false or numerical values
var a = 1;var b = '2'; if(a === b){ console.log(a); }else{ console.log(b); }
3. Use literals instead of new Array() This form
var arr = [];
4. Do not use global functions
5.Switch must Use the default branch
switch(num){ case 1: num++; breake; case 2: num--; breake; case 3: num = 0; breake; default: num = 1; }
6. Functions should not sometimes have a return value and sometimes not (it is recommended to have a return value: undefined)
7. For loops and if statements must use curly braces
for(var i=0 ;i<10; i++){ console.log(i); }
##8. The variables in the for in loop should use the var keyword to limit the scope, so as to avoid scope pollution
for(var i in obj){ console.log(obj); }The above are the basic specifications of JavaScript that I have compiled. I hope everyone will write code in the future. It is written strictly in accordance with coding standards.
Related articles:
JavaScript Date (date) related knowledge and usage
Introduction to the use of JavaScript RegExp objects
About how to use JavaScript Array (array) object
The above is the detailed content of Basic specifications of JavaScript. For more information, please follow other related articles on the PHP Chinese website!