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")I think what you mean to say is that Javascript doesn't have block scope for variables. It has blocks.
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 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).
Relatedly, to "fake" a block in JS, wrap it in a function:
[Edited for accuracy and clarity]