Skip to content

Coding Standards

Erfan Mostafiz edited this page Jul 31, 2021 · 1 revision

Coding Standards (JavaScript)

Naming Conventions:

Variable Name:

Variable name should be in lower case and for multiple word, variable name should be camelCase. Moreover, variable name should be self-descriptive. camelCase is used by JavaScript itself, by jQuery, and other JavaScript libraries.

For example:

let gender = ‘Male’; // If one word variable name
let firstName = 'Robin'; // If multiple word variable name


For Boolean Type variables a prefix like is /are/ or has helps to distinguish a Boolean from another variable.

For example :

let isVisible = true;

Constant Name:

Constants are intended to be non-changing variables in JavaScript which should be written in UPPERCASE letters.

For example:

let PI = 3.1416
let SECOND = 60

Function Name:

JavaScript functions will be written in camelCase. In addition, it's a best practice to actually tell what the function is doing by giving the function name a verb as prefix.

For exapmle:

function getTotal(a, b) {
return a + b;
}

This verb as prefix can be anything (e.g. get, fetch, push, apply, calculate, compute, post). It's yet another soft rule to consider for having more self-descriptive JavaScript variables.

Class Name:

We will declare a JavaScript class with PascalCase convention.

For example:

class OfficeStuffs {
constructor(name, age) {
this.name = name;
this.age = age;
}
}

let cleaner = new OfficeStuffs('Robin', 22);

Methods:

JavaScript functions are identified as a method when it is used in JavaScript class and it will be declared with camelCase. A verb as a prefix should be added for making the method name more self-descriptive.

For example:

class OfficeStuffs {
constructor(name, age) {
this.name = name;
this.age = age;
}
getName(){ // camelCasing Method Naming
return this.name
}
}

Code Indentation:

Use 4 spaces for indentation of code blocks.

For example:

if (condition) {
....statement1
} else {
....statement2
}

Line Length < 80 :

For readability, lines should not be longer than 80 characters. If a JavaScript statement does not fit in one line, the best place to break it, is after an operator or a comma.

For example:
Bad:

var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" };

Good:

var foo = {
"bar": "This is a bar.",
"baz": { "qux": "This is a qux" },
"easier": "to read"
};

Declaring String variables:

Use single quotes when declaring string type variables, unless you are writing JSON.

For example:
Good:

var foo = 'bar';

Bad:

var foo = "bar";

Condition statement:

  1. Use whitespace before and after the condition statement.
  2. Opening Braces will go on the same line as the condition.

Good:

if (condition) {
console.log('winning');
}

Bad:

if(true)
{
console.log('losing');
}

Notice the use of whitespace before and after the condition statement.

Declare one variable per var statement:

Declare one variable per var statement, it makes it easier to re-order the lines.

Right:

var keys = ['foo', 'bar'];
var values = [23, 42];
var object = {};
while (keys.length) {
var key = keys.pop();
object[key] = values.pop();
}

Wrong:

var keys = ['foo', 'bar'],
values = [23, 42],
object = {},
key;
while (keys.length) {
key = keys.pop();
object[key] = values.pop();
}

Clone this wiki locally