Code Standards And Conventions For JavaScript Programmers

Although, coding standards and general conventions are important, but in tools like JavaScript, where concept of code validation and or compilation is very weak, these things become very important. Generally, conventions like naming conventions, commenting and code indention may not give any benefit in performance (these will make your code easy to understand and easy to maintain), but coding standards can make a lot of difference.

I may recommend to review my previous article tips for JavaScript Programmers, from there you can find few important good practices to standardize JavaScript code.

Important Points

  • JavaScript is case sensitive.
  • Avoid special character in names. Dollar sign and underscore are exceptional cases where required.
  • Generally use camel case.
  • It is better to use complete descriptive name instead of abbreviations, except industry well know abbreviations and acronyms. Index variable like in for are also be an exception.
  • Use proper indention for coding, we generally use 4 character indention.
  • If possible the try to make your function limited to visibility of your screen. So in general 25 lines are more than enough.
  • Always consider minification and bundling.
  • We may try to avoid data type dependent naming.
  • We may try to use name similar to data provider. It is a debatable point as using names similar to data provide may cause security risk, but it will give you ease to maintain code.
  • Always address JavaScript Injection or Cross Site Scripting (XSS).
  • Use proper spacing between operators and operands.

File Name

If your application is going to be hosted on Linux environment then keep in mind file names are also case sensitive there. But it is not similar for Windows, in windows, file and directory names are not case sensitive. It is better to end JavaScript file to have .js extension.

Local Variable

We may prefer the following rules for local variables:

  • Use singular name in camel case for local variable of primitive type.
    var topicLength = 10000;
  • Use plural name in camel case for local variable of array and other collections.
    var coloNames = [“Red”, “Green”, “Blue”];

Global Variable

We may prefer the following rules for global variables:

  • Use singular name in pascal case for local variable of primitive type.
    var TopicLength = 10000;
  • Use plural name in pascal case for local variable of array and other collections.
    var ColoNames = [“Red”, “Green”, “Blue”];

Private Variable

We may prefer the following rules for private variables:

  • Use singular name in camel case starting with underscore for private variable of primitive type.
    var _topicLength = 10000;
  • Use plural name in camel case starting with underscore for private variable of array and other collections.
    var _coloNames = [“Red”, “Green”, “Blue”];

Public Function Name

For public functions, we may prefer to use camel case:

  1. function sayHello(person)  
  2. {  
  3.     return‘ Hello’ + person;  
  4. }  
Private Function Name

For private functions, we may prefer to use camel case starting with underscore:
  1. function _sayHello(person)  
  2. {  
  3.     return‘ Hello’ + person;  
  4. }  
Class Name

For classes, we may prefer to use constructor to create and initialize object (by the way, there is a good audience, whom are against new in JavaScript). We may prefer to use pascal case for class constructor name, but its variable may use camel case:
  1. function Car(carName, carModle, carColor)  
  2. {  
  3.     this.name = carName;  
  4.     this.modle = carName;  
  5.     this.color = carName;  
  6.   
  7.     function carData()  
  8.     {  
  9.         return this.name + ‘‘ +this.modle + ‘‘ +this.color;  
  10.     }  
  11. }  
  12. var myCar = new Car(‘Hona’, ‘City’, ‘Blue’);  
  13. alert(myCar.carData());  
Module Name

For modules, we may prefer to use pascal case:
  1. var Config = (function()  
  2. {  
  3.     var baePath = ‘’;  
  4.     var _getPath = function()  
  5.     {  
  6.         return _basePath;  
  7.     };  
  8.     var _setPath = function(basePath)  
  9.     {  
  10.         _basePath = basePath;  
  11.     };  
  12.     return {  
  13.         getPath: _getPath,  
  14.         setPath: _setPath  
  15.     };  
  16. })();  
if Statement

It is better to avoid single line if. I always recommend to use parenthesis with if and else part even there is single statement.
  1. if (condition)  
  2. {  
  3.     //do your action  
  4. }  
Loop Statement

It is better to avoid single line loop. I always recommend to use parenthesis with for even there is single statement.
  1. for (var i = 0; i <= 10; i++)  
  2. {  
  3.     //do your action  
  4. }  
Single Quotation or Double Quotation for String Values

There is no difference either you use single quotation or double quotations to represent a string. Even you can use both in a program, but it is recommended to use only one of them in a project. I personally recommend to use single quotation.

JavaScript Code Placement

Please consider the following recommendations for JavaScript placement in project:

 

  • It is always recommended to avoid inline JavaScript coding. Please consider placing JavaScript code in separate file/s. For file naming we may use camel case: viewName.js, controllerName.js, libraryName.js and etc.
  • It is better to place script reference and or code at bottom of HTML.
  • It is must to address JavaScript file caching at browser side.

return Statement

Make sure return statement and expression are on same line and end with semicolon.

Comment

For comments we may consider the following:

  • Always comment your code, particularly functions and complex scenarios.
  • It is better to use single line comments using // instead of block comment syntax using /* … */.
  • It is better to comment before code.
  • It is better to end comment with full stop.

Up Next
    Ebook Download
    View all
    Learn
    View all