Hacker News new | past | comments | ask | show | jobs | submit login

JavaScript doesn't have blocks. {} is shorthand notation for an object, so you would want something like

  function asdf(){
    return {x:1};
  }
It's a less verbose way of doing this:

  function asdf2(){
    var myObj = new Object();
    myObj.x = 1;
    return myObj;
  }
Outside of a object written in shorthand notation, x:1 is treated as a label (x:) followed by a value (1). In general labels are not used in JS, and it's probably a good idea to avoid them.

Relatedly, to "fake" a block in JS, wrap it in a function:

  (function(){
    var a = 1;
    // do something with a
  }());
  
  // out side of the "block" a is undefined (or whatever value it was before the "block")
[Edited for accuracy and clarity]



More precisely, JavaScript doesn't have block scope. It does have syntax for grouping multiple statements into "blocks" (not sure if that's the right word), e.x.

    if (...) {
        // block
    }
It turns out that blocks don't need to follow a control flow construct like "if", "while", etc, they can stand on their own anywhere a statement can.

What's happening in the above case is the syntax for an object literal and a block is ambiguous. It's normally not a problem since you don't have object literals standing on their own as statements, you do something with them as an expression. On a console (or any time you eval an expression) you should wrap it in a parenthesis to disambiguate it:

    ({x:1}) // expression (object with "x" property equal to "1")
    {x:1} // statement (label "x" followed by statement "1")


> "JavaScript doesn't have blocks"

I think what you mean to say is that Javascript doesn't have block scope for variables. It has blocks.


Yes, as several people have mentioned, I should have said block scope. JS does allow for blocks, but they don't work the way the parent was thinking.

You can toss extra curly braces in the code and it is a "block" but unless they are preceded by an "if", "for", or something similar, they don't do much. They do not return values and they do not affect variable scope. Functions do both, hence my last example.


I think my understanding of blocks is fine. "Returning" was the wrong expression, I meant the value of a script (like the result of an eval). I am aware of the lack of block scope in JS :-)


Gotcha, glad you've got it figured out then =]


Of course, a label! That explains it, thanks! I didn't have a JS reference handy at the time (and couldn't find it in the Mozilla ref in the short amount of time I had).

I have actually used labels in JS before, which resulted in a lot of questions from my colleagues.

I think the loop: for(...){break loop;} thing is acceptable (if for can not be avoided anyway).




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: