JavaScript Coding Guidelines
JavaScript Program Coding Guidelines:
- Always declare variable using var keyword, except when intentionally creating a global variable.
//var name=”Dhanik”;
- Always use semicolons at the end of statement. So that when minification will be done there will no lose of functionality.
- Constants should be uppercase, with each word separated by an underscore.
// var PI=2.147;
- Functions, variables, and method names all use camel case notation with the first letter lowercase.
//var empFirstName=”Dhanik”;
- Classes and enum names also use camel case notation but with the first letter uppercase.
- There should be proper spacing between statements. Use tabs to indent code. Don’t use white spaces at the end of lines and empty lines should not have spaces either. Below example illustrates the preferred spacing.
if ( name === “Dhanik” ) {
methodCall( “see”, “our”, “spacing” );
}
- Use proper comments. For multiple-line comments use /* */ and for single-line comments use //, with an empty line above the comment. The single-line comment precedes the comment that it refers to, and should be the only thing on the line.
// my comment
var name = ‘Dhanik;
var name = ’blah’; // bad
- Always use identity (===) comparison over simple quality (==). Simple equality should use when testing for null and undefined. Identity (===) equality will only check null.
- Use single quotes for string literals.
Comments
Post a Comment