Home > Web Front-end > JS Tutorial > body text

Basic specifications of JavaScript

亚连
Release: 2018-05-10 10:14:43
Original
2176 people have browsed it

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
Copy after login

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);
}
Copy after login

3. Use literals instead of new Array() This form

var arr = [];
Copy after login

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;
}
Copy after login

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);
}
Copy after login

##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);
}
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!