JavaScript Code Convention
All JavaScript projects should follow the same convention.
JavaScript Code Convention
Code conventions usually cover the following aspects:
- Naming rules for variables and functions
- Rules for using spaces, indentation, and comments.
- Other common conventions...
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
);
}
![]() |
Using the TAB key for indentation is not recommended, because different editors interpret the TAB key differently. |
|---|
Statement rules
General rules for simple statements:
- A statement usually ends with a semicolon.
Example:
var
values = [
"Volvo"
,
"Saab"
,
"Fiat"
];
var
person = {
firstName:
"John"
,
lastName:
"Doe"
,
age:
50
,
eyeColor:
"blue"
};
General rules for complex statements:
- Place the opening curly brace at the end of the first line.
- Add a space before the opening curly brace.
- Place the closing curly brace on its own line.
- End a complex statement with a semicolon.
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:
- Place the opening curly brace on the same line as the class name.
- Add a space between the colon and the property value.
- Use double quotes for strings, not needed for numbers.
- Do not add a comma after the last property value.
- Place the closing curly brace on its own line and end with a semicolon.
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:
- Variables and functions are identified in lower camelCase, that is, except for the first word, the first letter of each other word is capitalized ( lowerCamelCase)
- Global variables are in uppercase (**UPPERCASE **)
- Constants (such as PI) are in uppercase (**UPPERCASE **)
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).
![]() |
- 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.
![]() |
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.
