Left or Right? If it is JavaScript always Right (#1)

My Notes on JavaScript – The Good Parts by Douglas Crockford 

What is the ‘Good’ way to write JavaScript code? Douglas Crockford is talking about this topic in his book “JavaScript – The Good Parts” and in his video lectures at pluralsight .

Here are some points I learned by reading the book and watching the video lectures. Hope this will help you to clear some doubts you have.

Left or Right? If it is JavaScript always Right.

Left

return

{

name : cacoo

};

Right

return {

name : cacoo

};

These are the two common ways of putting opening curly braces. You can either put it a line below the return statement in left side or just after the return in right side.

You may have already heard, in JavaScript(JS) semicolons(;) are optional, it is trying to be smart and put semicolons where it thinks necessary. So in the Left scenario, it add the semicolon just after the return when compiling and return ‘undefined’ which is doing silently. So there will be a silent error in left side code which is difficult to find out.

So it is always advisable to put curly brace right side always as a practice to avoid all the confusions.

Advertisements

Author: Harshani Nawarathna

Sri Lankan woman in Computing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s