Introduction

Although coding standards and general conventions are important, in tools like JavaScript, where the 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 reviewing my previous article tips for JavaScript Programmers, from there you can find a few important good practices to standardize JavaScript code.

Important Points
File Name

If your application is going to be hosted on the 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 the JavaScript file to have a .js extension.

Local Variable


We may prefer the following rules for local variables:

Global Variable


We may prefer the following rules for global variables:

Private Variable


We may prefer the following rules for private variables:
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 an 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. function carData()
      7. {
      8. return this.name + ‘‘ +this.modle + ‘‘ +this.color;
      9. }
      10. }
      11. var myCar = new Car(‘Hona’, ‘City’, ‘Blue’);
      12. 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 a single line if. I always recommend using parenthesis with if and else part even there is a single statement.
          1. if (condition)
          2. {
          3. //do your action
          4. }
            Loop Statement

            It is better to avoid a single line loop. I always recommend using parenthesis with for even there is a 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 a 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 using a single quotation.

              JavaScript Code Placement

              Please consider the following recommendations for JavaScript placement in the project:
              return Statement

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

              Comment

              For comments we may consider the following: