The semicolon bug

Published Wed, Aug 26 2009 13:52

Since I’ve started blogging about JavaScript, I thought that it would be ok to share with you one of the most embarrassing bugs I’ve introduce in a code base. Now, when I look back, I do find it funny, but I can tell you that I didn’t laugh about it at the time. The code is really simple, but I guess it shows why I started putting braces on the opening line…

function doSomething(number) {
    //do some very interesting stuff here
    //return object
    return   
    {
        x: number + 1
    };
}    
var a = doSomething(1);
alert(a);

Really innocent, right? Unfortunately, it returns undefined instead of the literal object I was trying to build. Btw, this will only happen when the returned object only has one property. If you had more than one, then you’d be getting a parsing error (in JavaScript, it’s common to use anonymous objects for passing data around and it’s not uncommon to fill only the “used” fields).

I guess that if I had read the spec I would have found that JavaScript will automatically insert the ; after certain statements. Here’s what the 1.3 spec says about this topic:

Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

I guess that the moral of the story is that in JavaScript, the C K&R style should be used always.

P.S.: a final note to say that I’ll be receiving Douglas Crockford’s book in the next couple of days. That means I’ll be learning a few more tricks to share with you :)

Filed under:

Comments

# Arnis L. said on Wednesday, August 26, 2009 8:56 AM

Just don't stop. Not so often i can see stuff so valuable and learn with such an ease. JS always seemed like a swamp. Finally it's getting better. :)

# Ram said on Wednesday, August 26, 2009 2:45 PM

Here's another incredible ans extremely innocent bug: if you end a comment with an accent, for exemple "// ceci est commenté"... the code breaks LOL

# luisabreu said on Thursday, August 27, 2009 3:14 AM

Ram, can you put a small snippet that shows this and on which browsers do you see that problem? thanks.

# Ram said on Thursday, August 27, 2009 7:57 AM

OK -- if you save this snippet with Notepad (or VS) it breaks under IE6 (and only IE6):

<html>

<head>

<script>

// Ceci est commenté

function Hello() { alert(42); }

</script>

</head>

<body onload="Hello()"></body>

</html>

...this is the most innocent and hard to find bug I've ever met :) the reason is when you save the file you have to set the encoding to UTF-8. I hate IE6 LOL

# Ram said on Thursday, August 27, 2009 8:05 AM

BTW I absolutely love you blog, it reminds me this awesome article from MSDN Mag: msdn.microsoft.com/.../cc163419.aspx

# luisabreu said on Thursday, August 27, 2009 8:05 AM

oh, IE 6 :) I'm one of the happy guys which don't have to support it! :)

# Arnis L. said on Thursday, August 27, 2009 8:57 AM

Neither do I. :)

http://www.saveie6.com/

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above:  

Search

This Blog

Tags

Community

Archives

Syndication

Email Notifications

News




  • View Luis Abreu's profile on LinkedIn


    Follow me at Twitter

    My books

    Silverlight 4.0: Curso Completo

    ASP.NET 4.0: Curso Completo

    Portuguese LINQ book cover

    Portuguese ASP.NET 3.5 book cover

    Portuguese ASP.NET AJAX book cover

    Portuguese ASP.NET AJAX book cover