Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Coding Style

Shashank Garg edited this page Jan 3, 2014 · 1 revision
  • JavaScript code should not be embedded in HTML files unless the code is specific to a single session.
  • The unit of indentation is four spaces. Use of tabs should be avoided.
  • Max line length allowed is 80 characters.
  • Obvious comments and debugging comments must be avoided.
  • Use multiline comments /comment/ for Doc-string of functions.
  • Function and variable names should be in lower camel case like meraFunction or meraVariable.
  • Class names should be in upper camel case like MeriClass.
  • In if statements, for loops, while loops and functions. Use one space after right parentheses and left curly bracket . The right curly brace should be aligned with the line containing the beginning of the declaration. Eg.
function outer(c, d) {
        var e = c * d;

        function inner(a, b) {
            return (e * a) + b;
        }

        return inner(0, 1);
    }

if (condition) {
    statements
}

for (initialization; condition; update) {
    statements
}

while (condition) {
    statements
}

Blank spaces

Blank spaces should be used in the following circumstances:

  • A keyword followed by ( (left parenthesis) should be separated by a space. while (true) {
  • A blank space should not be used between a function value and its ( (left parenthesis). This helps to distinguish between keywords and function invocations.
  • All binary operators except . (period) and ( (left parenthesis) and [ (left bracket) should be separated from their operands by a space.
  • No space should separate a unary operator and its operand except when the operator is a word such as typeof.
  • Each ; (semicolon) in the control part of a for statement should be followed with a space.
  • Whitespace should follow every , (comma).

For any confusion see this.

Code can be tested for all coding style related errors here and here

Clone this wiki locally