Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Code Convention

JavaScript Code Convention


All JavaScript projects should follow the same convention.


JavaScript Code Convention

Code conventions usually cover the following aspects:

Well-formatted code is easier to read and maintain.

Code conventions are usually defined before development and can be agreed upon with your team members.


Variable Name

Variable names are recommended to use camelCase (camelCase):

firstName =

"John"

;

lastName =

"Doe"

;

price =

19.90

;

tax =

0.20

;

fullPrice = price + (price * tax);


Spaces and operators

Usually spaces should be added before and after operators ( = + - * / ):

Example:

var

x = y + z;

var

values = [

"Volvo"

,

"Saab"

,

"Fiat"

];


Code indentation

Usually 4 spaces are used to indent code blocks:

Functions:

function

toCelsius(fahrenheit) {

   

return

(

5

/

9

) * (fahrenheit -

32

);

}

Note Using the TAB key for indentation is not recommended, because different editors interpret the TAB key differently.

Statement rules

General rules for simple statements:

Example:

var

values = [

"Volvo"

,

"Saab"

,

"Fiat"

];

var

person = {

    firstName:

"John"

,

    lastName:

"Doe"

,

    age:

50

,

    eyeColor:

"blue"

};

General rules for complex statements:

Functions:

function

toCelsius(fahrenheit) {

   

return

(

5

/

9

) * (fahrenheit -

32

);

}

Loop:

for

(i =

0

; i <

5

; i++) {

    x += i;

}

Conditional statements:

if

(time <

20

) {

    greeting =

"Good day"

;

}

else

{

    greeting =

"Good evening"

;

}


Object rules

Rules for object definitions:

Example:

var

person = {

    firstName:

"John"

,

    lastName:

"Doe"

,

    age:

50

,

    eyeColor:

"blue"

};

Short object code can be written on one line:

Example:

var

person = {firstName:

"John"

, lastName:

"Doe"

, age:

50

, eyeColor:

"blue"

};


Fewer than 80 characters per line of code

For readability, it is recommended to keep fewer than 80 characters per line.

If a JavaScript statement exceeds 80 characters, it is recommended to break the line after an operator or comma.

Example:

document.getElementById(

"demo"

).innerHTML =

   

"Hello Runoob."

;

Try it »


Naming conventions

The naming conventions of many programming languages are similar, for example:

Naming variables: do you use these conventions: hyp-hens, camelCase, or under_scores ?

The hyphen (-) character in HTML and CSS:

HTML5 attributes can be prefixed with data- (e.g., data-quantity, data-price).

CSS uses - to join property names (font-size).

Note - is usually considered subtraction in JavaScript, so it is not allowed.

Underscores:

Many programmers prefer to use underscores (e.g., date_of_birth), especially in SQL databases.

The PHP language usually uses underscores.

PascalCase:

PascalCase is more common in the C language.

CamelCase:

CamelCase is generally recommended in JavaScript, and jQuery and other JavaScript libraries use camelCase.

Note Variable names should not start with $, as it conflicts with many JavaScript libraries.

HTML loading external JavaScript files

Use a concise format to load JavaScript files (the type attribute is not required):

<

script

src=

"myscript.js"

A poorly formed HTML document may cause JavaScript execution errors.

The following two JavaScript statements produce different results:

Example

var

obj = getElementById(

"Demo"

)

var

obj = getElementById(

"demo"

)

Try it »

HTML and JavaScript should use the same naming conventions as much as possible.

Accessing HTML(5) code conventions。


File extension

HTML file extensions can be .html (or .htm)。

CSS file extension is .css 。

JavaScript file extension is .js 。


Use lowercase file names

Most web servers (Apache, Unix) are case-sensitive: london.jpg cannot be accessed as London.jpg.

Other web servers (Microsoft, IIS) are case-insensitive: london.jpg can be accessed as London.jpg or london.jpg.

You must maintain a consistent style, and we recommend using lowercase file names consistently.