JavaScript Best Practices

1. Use === Instead of ==

The == operator evaluates the two items based upon their value.
The === operator evaluates the two items based upon their type and value.

you should always use triple equals (===).

Example

1 == “1”;  // true
1 == true;  // true

1 === “1”;  // false
1 === true;  // false

2. Use Shortcut Notations

Shortcut notations keep your code snappy and easier to read once you get used to it.

Example

var book = new Array();
book[0]=’Math;
book[1]=’Science';
book[2]=’History';
book[3]=’English';
is same as
var book = [ ‘Math’, ‘Science’, ‘History’, ‘English’];
if (v) {
var x = v;
} else {
var x =10;
}
is same as
var x = v || 10;
var direction;
if ( x  > 100){
direction = 1;
} else {
direction = -1;
}
is same as
var direction = (x > 100) ? 1 : -1;

3.  Always Use Semicolons

 Most developers won’t intentionally fail to put semicolons in the proper places.

In some cases, the compiler might assume that a semicolon is not needed, which will introduce tricky, hard-to-find bugs into your code.

A good tool to help you check your JavaScript for forgotten semicolons is JSLint.

Example:

Bad:

var demo = ‘some string’
function doSomething() {
return ‘something’
}

Good:

var someItem = ‘some string';
function doSomething() {
return ‘something';
}

4. Use [] instead of new Array()

The same applies for creating a new array.

Okay:
var a = new Array();
a[0] = “Name”;
a[1] = ‘Age';

Better:

var a = [‘Name’,’Age’];

5. Use {} Instead of New Object()

The same applies for creating a new object.

Bad:

var o = new Object();
o.name = ‘Nitish';
o.lastName = ‘Kumar';
o.getName= function() {
return this.name;
}
However, this method receives the “bad practice” stamp without actually being so

Better
var o = {
name: ‘Nitish’,
lastName = ‘Kumar’,
getName: function() {
return this.name;
}
};

6. Use Parameter Defaults

If a function is called with a missing argument, the value of the missing argument is set to undefined.

Undefined values can break your code. It is a good habit to assign default values to arguments.

Example

function myFunction(x, y) {
if (y === undefined) {
y = 0;
}
}
Or, even simpler:

function myFunction(x, y) {
y = y || 0;
}

7. Prefixing every new variable in a given scope with var

There’s no point in having several var statements if you only need one:
var someVar1 = ‘a';
var someVar2 = ‘b';
var someVar3 = ‘c';

// Much easier:
var someVar1 = ‘a’,
someVar2 = ‘b’,
someVar3 = ‘c';

8. Avoid Using eval()

The eval() function is used to run text as code. In almost all cases, it should not be necessary to use it.

Tt allows arbitrary code to be run, it also represents a security problem.

For those unfamiliar, the “eval” function gives us access to JavaScript’s compiler. Essentially, we can execute a string’s result by passing it as a parameter of “eval”.

This decrease your script’s performance substantially, and it also poses a huge security risk because it grants far too much power to the passed in text. Avoid it!

9. Avoid use of  “For in” Statements

When looping through items in an object, you’ll also retrieve method functions as well. It always wrap your code in an if statement which filters the information.

It does not simply walk down the index list like a basic for loop does. The following two examples are NOT equivalent:

// The standard for loop
for(var i = 0;  i < arr.length;  i++) { }

// The for…in loop
for(var i in arr) {}

10. Dono’t use array.length in condition for the loop

Bad:

for (var  i = 0;  i < array.length;  i++ ) { … }

Loop will calculate array length each time. So this will cause an enormous overhead.

Better:

for (var  i = 0, len = array.length;  i < len;  i++ ) { … }

 

13 thoughts on “JavaScript Best Practices

  1. I find it sad that such a blog post should ever need to be made. It indicates a couple of very important things.

    1. Many people who call themselves programmers shouldn’t.
    2. Javascript is a broken language. Such tips and tricks shouldn’t ever be necessary for a language that isn’t broken.
    3. Due to [1] and [2] the brokeness has little chance of being rectified until such time as a non broken language replaces Javascript.

  2. I read a lot of interesting posts here. Probably you
    spend a lot of time writing, i know how to
    save you a lot of time, there is an online tool that creates unique, SEO friendly articles in seconds, just type
    in google

  3. It was difficult to find your website in google. I found
    it on 21 place, you should spread your articles to social websites , it will
    help you to get much more visitors. You can do it on autopilot.

  4. We tried out taking a look at your web site with this i-pod touch as well as the configuration will not are generally suitable. Might choose to find out about it upon WAP in addition to seems like most mobile phone cool layouts aren’t going to be really dealing with your website.

Comments are closed.