Javascript-Coding Style
Coding conventions secure quality:
- Improves code readability
- Make code maintenance easier
Syntax:

Variable Names:
You use camelCase for identifier names variables and functions.
All names start with a letter.
Syntax:
firstName = "I2tutorials"; lastName = "javascript"; price = 19.90; tax = 0.20; fullPrice = price + (price * tax);
Spaces Around Operators:
Always put spaces around operators ( = + – * / ), and after commas:
Syntax:
var x = y + z; var values = ["java", "css", "html"];
Code Indentation:
Always use two spaces for indentation of code blocks:
Syntax:
function toCelsius(fahrenheit) {
return (5 / 9) * (fahrenheit - 32);
}
Statement Rules:
Always end a simple statement with a semicolon.
Syntax:
var values = ["html", "css", "javascript"];
var person = {
firstName: "I2",
lastName: "Tutorials",
age: 50,
eyeColor: "green"
};
General rules for complex or compound statements:
- Place the opening brackets at the end of the first line.
- Use one space before the opening brackets.
- Place the closing brackets on a new line, without leading spaces.
- Do not end a complex or compound statement with a semicolon.
Functions:
function toCelsius(fahrenheit) {
return (5 / 9) * (fahrenheit - 32);
}
Loops:
for (i = 0; i < 5; i++) {
x += i;
}
Conditionals:
if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
Object Rules:
- Place the opening brackets on the same line as the object name.
- Use colon plus (: +)one space between each property and its value.
- Use quotes (” “,’ ‘)around string values, not around numeric values.
- Do not add a comma(,) after the last property-value pair.
- Place the closing brackets on a new line, without leading spaces.
- Always end an object definition with a semicolon(;).
Example
var person = {
firstName: "I2",
lastName: "Tutorials",
age: 50,
eyeColor: "green"
};
Naming Conventions:
- Variable and function names are written as camelCase.
- Global variables are written in UPPERCASE (We don’t, but it’s quite common).
- Constants (like PI) written in UPPERCASE.