Introduction
Code style is about consistency and discipline, and there's no real right or wrong. The first step would be to adhere to the style of the used platform and stick to that. It helps others reading your code, and helps you read the code of others in the same ecosystem, and can only be decided by personal preference. Guidelines posted here are not rules that you should comply to, but consider them as suggestions that can improve your code quality
Consistency is the key to maintaining code. This statement is most true for naming your projects, source files, and identifiers including variables, properties, methods, parameters, classes, interfaces, and namespaces.
Naming Conventions
| Identifier Type | Rules for Naming | Example |
| Namespaces | Pascal Case; should start with the company namespace, followed by application "layers" as applicable | Mcn.Contacts |
| Classes | Nouns in Pascal Case | class Mcn.Contacts.ManageContactsViewModel |
| Functions | Verb or Verb/Noun pair in Camel Case | initializeData (); |
| Constants | All uppercase with words separated by underscores | var MAX_CONTACTS_TO_DISPLAY = 200; |
| Instance Variable | Camel Case | var currentContacts = null; |
| Local Variable | Camel Case | var callBacks = {}; |
| Parameters | Camel Case | function (contactSearchFilter) |
Case sensitivity
- Two namespaces with names that differ only by case.
- Functions with names that only differ by a single letter or number.
- A type with method names that differ only by case.
- Variables with a type that differs only by case.
- Variables matching data type using a different case.
Coding Style
Formatting
- Always use a namespace for a JavaScript file
- Never declare more than 1 namespace per file
- Avoid putting multiple "classes" in a single file
- Always place curly braces ({ and }) on a new line
Example
- var contactsAvailable = function ()
- {
- };
-
Always use curly braces ({ and }) in conditional statements.Example
- if (contactCount === 0)
- {
- // code for true case
- }
- else
- {
- // code for false case
- }
-
Always put parenthesis around all clauses in if statements.ExampleBadGood
- if (contactCount === 0 && retrievingData === true)
- {
- }
- if ((contactCount === 0) &&
- (retrievingData === true))
- {
- }
-
Never make function calls within a clause of if statements.ExampleBadGood
- if (getContacts().Count == 0)
- {
- }
- var contactList = getContacts();
- if (contactList.Count == 0)
- {
- }
-
Do not use elseif to string if statements together.
-
Always use a Tab and Indention size of 2, inserted as spaces.
-
Declare each variable independently, not in the same statement.ExampleBadGood
- var firstName, lastName;
- var firstName
- var lastName;
-
Recursively indent all code blocks contained within braces. NOTE: Visual Studio supports formatting code by Edit/Advanced/Format Code (or control-K, control-D). This requires setting the Editor tabbing and spacing as indicated above.
-
Use white space (CR/LF, Tabs, and so on) liberally to separate and organize code.
-
If in doubt, always err on the side of clarity and consistency.
-
Parameters in method declarations should be on separate lines and all parameters should be left-aligned.Example
- var getPhoneNumber = function (contactData,phoneNumberIndex)
- {
- };
-
Parameters in method calls should be on separate lines with all parameters left-aligned.Example
- getPhoneNumber(contactData phoneNumberIndex);
-
Do not insert spaces in a parameter list for method declarations or method calls NOTE: This behavior is configurable in Visual Studio.Example
- getPhoneNumber(contactDataphoneNumberIndex, dbConn, dbTrans);
-
Declare all variables at the beginning of a function. Do not place declarations throughout the function body.

Jaganathan BantheswaranPosted Dec 11, 2013, 4:22 AM
Thanks for your update
AnkurPosted Dec 11, 2013, 1:51 AM
Thanks jaganathan.. I made the corrections..also put a note in "introduction" section saying these guidelines are suggestions.
Jaganathan BantheswaranPosted Dec 10, 2013, 11:50 PM
If this article is kind of suggestions, then please recheck about the Title. Javascript Coding Standard means that the coding standard for javascript which globally accepted & SHOULD be followed by Javascript developer. And, there is NO List<> type of constructs, please recheck it
AnkurPosted Dec 10, 2013, 10:34 PM
hi Jaganathan..thanks for the comment....Code style is about consistency and discipline, and there's no real right or wrong. The first step would be to adhere to the style of the used platform and stick to that. It helps others reading your code, and helps you reading the code of others in the same ecosystem, and can only be decided by personal preference. Guidelines posted here are not rules that you should comply to, but consider them as suggestions that can improve your code quality.
Jaganathan BantheswaranPosted Dec 10, 2013, 6:53 AM
I am NOT satisfied with the following points. Please recheck it once again. 1.Always place curly braces ({ and }) on a new line 2. Always put parenthesis around all clauses in if statements. 3.Declare each variable independently, not in the same statement. 4.Parameters in method declarations should be on separate lines and all parameters should be left-aligned. 5.Parameters in method calls should be on separate lines with all parameters left aligned. 6.Declare all variables at the beginning of a function. Do not place declarations throughout the function body. 7.Use // or /// but do NOT use /* */