javascript funkyness

Posted on August 22nd, 2010 by thiswayup.
Categories: dev.

As I've started to learn more about javascript this year , I've been really enjoy digesting some interesting resources on the internet to learn more about the innards that make this rather great lanaguage. One that I have found is a great google tech talk "JavaScript: The Good Parts" from Doug Crockford and he highlighted a rather quiky thing in javascript.

function good(){
   return true
}
alert( good() );
//alert is true

function bad(){
   return
   true
}
alert( bad() );
//alert is "undefined";

One would think that if you run both function you would get the same return (ie true) but there's one thing I learnt from Doug's talk is that Javascript was built with a interesting quirk, if it runs into a compilation error, it actually backs up and puts in a semi colon in for you! This was originally designed to help people first picking up the language but as a consequence has caused this weird business. So the moral is to always add semi colons.

2 comments.