When you follow the style guidelines for a language that you are working with, the code becomes more readable and maintainable.
Let’s go over some guidelines for Javascript from the Google JavaScript Style Guide:
// class name in UpperCamelCase
class MyClass {
// Class methods in lowerCamelCase
constructor() { ... }
// Parameters in lowerCamelCase
methodOne(numberOne) { ... }
}
// Local variable name in lowerCamelCase
let myVariable = 20;
// Constant in UPPERCASE
const PI = 3.142;
// Braces around the control structure for.
// Spaces around the operators
// Line break after the opening and closing curly brace
for(int i = 0; i < 10; i++){
// Statement indented by two spaces.
console.log("Hello World!");
}
{ }
, instead of the object constructor.// Using the { } object literal
let country = {
// Using unquoted keys only
Name: "Singapore",
Area: 721.5,
};
let
when a variable needs to be reassigned and const
otherwise. Do not use var
.// Using let because myVar needs to be reassigned late
// Moreover, only one variable is declared per line
let myVar = 10;
let anotherVar = 19;
myVar = 20;
// Using const because welcome does not need to reassigned.
const welcome = "Hello";
Refer to the Google JavaScript Style Guide for more details.