Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

JavaScript | Style Guide and Coding Conventions

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The JavaScript style guidelines are directions to regulate programming in javascript. These coding prescription are beneficiary to write code in javascript without any syntactical errors. It covers the naming of variables, direction to put whitespaces and semicolons and various statement guidelines. 
It also can improve the Quality, Readability & make code maintenance easier.

This article basically covers coding conventions like: 

  • Indentation
  • Spaces, all brackets
  • Variable declaration
  • Object guidelines
  • CamelCasing for identifier names
  • Loops and control statements
  • Statement guidelines
  • Constructor
  • Comments

1. Indentation: 

We always indent our code using two spaces & our code must not end with trailing whitespaces. 


function xyz{

2. Spaces, all brackets: 

Blank spaces should always be used in the following circumstances: 

  • A keyword followed by ‘(‘ or ‘)’ must provide whitespaces before and after it.
    if (condition) {
  • All keywords like ‘function’, ‘typeof’ etc needs extra attention for whitespaces & a space provided after it. 


let integer = function (
) {
    value = resolve(value);
    return (10*value);

3. Variable declaration: 

All variables must be declared with var before they are used. we mostly declare the variable at the beginning of the function. 
All names start with a letter. 


Fullform = "GeeksforGeeks";
Rate = 19.90;
Hate = 0.20;
Ranking = Rate + (Rate / Hate);

4. Object guidelines : 

General rules for object definitions: 
There are various types of objects in javascript as array, function, dates, user define objects. 
Basically, every object follows a simple coding style written below. 
In Javascript, we put an equal(=) & opening bracket after declaring the name of the object. We define property as using a colon: and put string values inside quotes “xyz” but not on numeric value. We put a semicolon at the end of one property and do not put at the end of last property.


var platform = {
  firstName: "GEEKS",
  lastName: "GEEKS",
  Color: "GREEN"
//some are compressed can be written as
var platform = {firstName:"GEEKS", lastName:"GEEKS", Color:"GREEN"};

5. Camel casing for identifier names: 

We mostly use camelcase for the identifier. In camel casing, the first letter of each function should be lowercase and the first letter of subsequent words should be capitalized. Example 


function myFunction(a, b) {
  return a * b;

There should not be underscores between the words. 
If you use a mix of upper and lower case, you have to be extremely consistent & careful.

6. Loops and Control Structures:

We use same convention of parenthesis & brackets for control structures as discussed below 

  • Always follow whitespace after an identifier of control statements. 
  • Always provide whitespace & linebreak after the ; semicolon. 
  • Always provide a whitespace after every, coma . 

example given below :-


if (condition1 || condition2) {
else if (condition3 && condition4) {
else {

7. Statement Guidelines: 

  • Simple statement:
    The statement which consists only single line Always end with a semicolon.
  • Compound statement:
    In javascript, we put the space & opening bracket { and have a list of statements with closing bracket } at the end. 
    for ex:- at the end of function name declaration before defining it. 
    We put the closing bracket on a new line without following whitespace trail. 
    We do not put the semicolon at the end of these type of statements.
  • Multiline statements:
    In javascript Whenever a statement is not small enough to fit on one line, 
    line breaks must occur after an operator. 
    To improves readability lines should break into multiple lines.


//simple statement 
var gfg = ["Geeks", "For", "Geeks"];
//compound statement
for (i = 0; i < 5; i++) {
  a += i;
//multiline statement
if (
  a === 123
  && b === 'abc'
) {

8. Constructor:

In javascript, Constructors are used in form of function with new keyword. 
The newprefix creates a new object and binds that object to the function’s implied this parameter. 
Constructor functions MUST be given names with an initial uppercase character. 


function GeeksforGeeks(node) {}
var geeksforgeeks= new GeeksforGeeks(element);


Use line comments, not block comments. The comments should start at the left margin. 
Use ‘//’ in start of comments. 


// Set i to zero.
    i = 0;

My Personal Notes arrow_drop_up
Last Updated : 28 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials