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
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
- JavaScript is case sensitive.
- Avoid special characters in names. The dollar sign and underscore are exceptional cases where required.
- Generally, use camel case.
- It is better to use a complete descriptive name instead of abbreviations, except the industry well knows abbreviations and acronyms. Index variables like in for are also an exception.
- Use proper indention for coding, we generally use 4 character indention.
- If possible the try to make your function limited to the 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 the name similar to data providers. It is a debatable point as using names similar to data provide may cause a 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 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.
We may prefer the following rules for local variables:
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:
- Use a singular name in camel case for the local variable of primitive type.
var topicLength = 10000; - Use a plural name in camel case for the local variable of the array and other collections.
var coloNames = [“Red”, “Green”, “Blue”];
Global Variable
We may prefer the following rules for global variables:
- Use a singular name in the pascal case for the local variable of primitive type.
var TopicLength = 10000; - Use a plural name in the pascal case for the local variable of the array and other collections.
var ColoNames = [“Red”, “Green”, “Blue”];
Private Variable
We may prefer the following rules for private variables:
- Use a singular name in camel case starting with an underscore for the private variable of primitive type.
var _topicLength = 10000; - Use a plural name in the camel case starting with an underscore for the private variable of the array and other collections.
var _coloNames = [“Red”, “Green”, “Blue”];
Public Function Name
For public functions, we may prefer to use camel case:
For public functions, we may prefer to use camel case:
- function sayHello(person)
- {
- return‘ Hello’ + person;
- }
For private functions, we may prefer to use camel case starting with an underscore:
- function _sayHello(person)
- {
- return‘ Hello’ + person;
- }

Asfend YarPosted Feb 28, 2016, 1:43 AM
good
Sr KarthigaPosted Feb 8, 2016, 9:01 AM
Good one