JavaScript Best Practices

Here are some points which are consider as best practices.

Use easy, short and readable variable and function names

Always use easy to understand and readable names for variable and function names.

Bad variable names

x1, xyz, fe2

Below are also bad variable names as they are too long.

incrementFromOneToTwenty
totalOfTwoMonthsWages

Above variables names are too long and should be avoided. Same rules apply for function names. Too long or Too short function names should be avoided.

Avoid Globals

Avoid use of global variables as much as possible.

Reason to not use global variable

Use of global variables in JavaScript is vulnerable against other scripts. Any external scripts can modify value of global variables which are added after your script is added. Also global variables can be modified by external script during function calls if same variable name is used.

Declare Local Variables

Variables used within functions must be declared as local variables.
Local variables must be declared using var keyword otherwise it will be treated as global variables.

It is advisable to use strict mode, it is not allowed to use undeclared variables.

Put Declarations at the Top

It is a good practice to declare variables before each script or functions definitions.

  • It makes code looks clear.
  • Makes debugging easy, as require single place to check for local variable
  • Unwanted global variables can be avoided.

Initialize Variables

It is a good practice to initialize variables during declaration.

It will provide cleaner code, single place for initialization and avoid undefined values for variables which can cause unexpected results.

Initializing variables gives more idea about use of variables and its data type.

Do not use Objects to declare Number, Strings or Boolean

Use numbers, strings and Boolean values as primitive type values, and not as objects. Objects will slow down execution speed and sometimes provide unexpected results.

Example

var fname="";
var firstName = new String("Kevin");
fname === firstName // returns false as data type of both variable is different.

Also it is not possible to compare two objects.

var firstName = new String("Kevin");
var myName = new String("Kevin");

firstName == myName // returns false as objects cannot be compared.

Don’t Use new Object()

  • Do not use new Object(), use {} instead
  • Do not use new String(), Use “” instead
  • Do not use new Number(), Use 0 instead
  • Do not use new Boolean(), Use false instead
  • Do not use new Array(), Use [] instead
  • Do not use new RegExp(), Use /()/ instead
  • Do not use new Function(), use function (){} instead

Be cautious with Automatic Type Conversions

Numbers can be automatically converted to Strings or NaN.

As JavaScript is a loosely typed language, a variable can change its data type and contain different data types.

Example

var address = '1/4, New Building'; // address is string
address = 50;  // address is now number
<<< JavaScript ConventionsJavaScript Common Mistakes >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .