Stupid facts about JavaScript #1

During the first week of learning JavaScript I have found a few things that just confused me, things in which I could not believe until I test it, and today I am going to share some of them with you.

All of you familiar with holy wars at work regarding “Java or C#”, “comment or not comment”, “test or not to test”, “spaces vs tabs” etc. It’s like “to be or not to be” for developers, but it’s almost nothing in comparison with next lines of code:

var func = function(){
 // some logic here

 return {
 //some result here
 }
}

<strong>VS</strong>

var func = function(){
 // some logic here

 return
 {
 //some result here
 }
}

What might be wrong here? Does it really matters what code style we are using in this case and where that curly bracket is? It looks like in first example we are using space more efficiently, but second is easier to read as we see where this objects starts and ends. We might talk about for an hours but the biggest problem with this code is that second example just doesn’t work. Ok it works, we will never get any error but the method will just return “undefined”
This is the moment where you should say “No way!”, press Ctrl+Shift+I in your browser and test it by your self.
After that with a bit confused face you should admit “it doesn’t”.

So what is the reason of such a behavior? Short answer is: they just wanted to make javascript “simpler”, as juniors very often forgot to put semicolon at the end of the row. To solve this they have added auto insertion of the semicolon at the end of the rows where they might be forgotten, which works well in most cases, but in this it just fails, as we might use return as operator without any arguments.

See you soon
Cheers

Advertisements

2 thoughts on “Stupid facts about JavaScript #1

  1. Yes it might cause some problems but it can be easily handled. In any case it’s very interesting language and even the history of language itself is also interesting so stay tuned, more to come!

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