Javascript Coding Standards and Best Practices
December 20, 2016 | Author: Aakash Urvesh Thakkar | Category: N/A
Short Description
Required standards for Javascript and some best practices while coding in javascript according to W3Schools...
Description
JavaScript Coding Standards and Best Practices Version 1.0
Document Revision History:
Prepared By: Aakash Thakkar
Version
Primary Author(s)
1.0
Aakash Thakkar
Description JavaScript Coding Standards and Best Practices (First Draft)
Date 12May2015
Introduction JavaScript first appeared almost 20 years ago in 1995, and as of today it has continuously evolved since and is most commonly used as part of web browsers , whose implementations allow clientside scripts to interact with the user , control the browser, communicate asynchronously , and alter the document content that is displayed. It is also used in serverside network programming with runtime environments such as Node.js , game development and the creation of desktop and mobile applications.
This document is prepared for you to follow the best practices while writing JavaScript code. Many organizations such as W3Schools, SUN(now Oracle) and Wordpress have released JavaScript coding standards. In this document, I will be covering the best practices and coding standards published by W3Schools.
This document will cover the following : 1.
Naming and declaration rules for variables and functions.
2.
Rules for spaces, indentations and comments.
3.
Practices and principles.
By following the coding standards and best practices it will make code readability and maintenance easier.
Standards 1. Naming Conventions The W3 standards suggests to use camelCase for declaring variables and functions. UPPERCASE for golbal variables and constants.
firstName = "John"; lastName = "Doe"; constMYCONSTANT = 7;
2. Spaces around operators Always put spaces around operators (=, +, , *, /), and after commas.
varx =y +z;
varvalues = [ "Volvo" , "Saab" , "Fiat" ];
3. Code Indentation
Always use 4 spaces for indentations for code blocks, and don’t use TAB key, as different editors use tab key differently.
functiontoCelsius ( fahrenheit ){ }
return ( 5 / 9 ) * ( fahrenheit - 32 );
4. Statement Rules Always end a simple statement with a semicolon.
varvalues = [ "Volvo" , "Saab" , "Fiat" ]; General rules for Compound statements : 1.
Put the opening bracket at the end of the first line.
2.
Use one space before the opening bracket.
3.
Put the closing bracket in a new line, without leading spaces
4. Do not end a complex statement with a semicolon. varperson ={
firstName : "John", lastName : "Doe", age : 50, eyeColor : "blue" };
Functions:
Loops:
functiontoCelsius ( fahrenheit ){
for ( i = 0 ;i
View more...
Comments