i2tutorials

Javascript-Coding Style

Javascript-Coding Style

 

Coding conventions secure quality:

 

 

Syntax:

 

Javascript-Coding Style

 

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:

 

 

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:

 

 

Example

 

var person = {
 firstName: "I2",
 lastName: "Tutorials",
 age: 50,
 eyeColor: "green"
};

 

Naming Conventions:

 

Exit mobile version