HTML5 Zone is brought to you in partnership with:

Merrick Chaffer is a Software Developer with Russell Reynolds Associates, with over 16 years experience in areas relating to Microsoft Application Development. Merrick is a DZone MVB and is not an employee of DZone and has posted 37 posts at DZone. You can read more from them at their website. View Full User Profile

Beware the Perils of JavaScript

11.03.2012
| 2102 views |
  • submit to reddit

These two links are definitely worth a read I think...

http://oreilly.com/javascript/excerpts/javascript-good-parts/bad-parts.html

and especially…

http://oreilly.com/javascript/excerpts/javascript-good-parts/awful-parts.html

e.g.

Semicolon Insertion

JavaScript has a mechanism that tries to correct faulty programs by automatically inserting semicolons. Do not depend on this. It can mask more serious errors.

It sometimes inserts semicolons in places where they are not welcome. Consider the consequences of semicolon insertion on thereturn statement. If a return statement returns a value, that value expression must begin on the same line as the return:

return
{
status: true
}; 

This appears to return an object containing a status member. Unfortunately, semicolon insertion turns it into a statement that returns undefined. There is no warning that semicolon insertion caused the misinterpretation of the program. The problem can be avoided if the { is placed at the end of the previous line and not at the beginning of the next line:

return {
status: true
};




Published at DZone with permission of Merrick Chaffer, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)