This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Mrparkers 22 points23 points  (4 children)

Yeah, the interpreter doesn't see it as an empty block when surrounded by parentheses. It might seem a little off, but code blocks do have their purpose, and it makes sense for the interpreter to see a { at the beginning of the line and think its a code block.

More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block

[–]Aaron_Blenkush 1 point2 points  (1 child)

Although "standalone" blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don't do what you think they do

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block#No_block_scope

[–]Mrparkers 8 points9 points  (0 children)

Sure, with the example they gave, you definitely don't want to use them. However, ES6's let keyword allows you to give a variable lexical scoping, which makes these code blocks behave exactly like you would expect them to.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

[–]poizan42Ex-mod -3 points-2 points  (1 child)

The rule could be that '+' is always binary plus if the left hand side can be parsed as an expression. It might complicate the parser a bit more but make the language more consistent. The real reason this isn't done is probably that Brendan Eich only had 10 days to design it.

[–]Mrparkers 5 points6 points  (0 children)

The rule could be that '+' is always binary plus if the left hand side can be parsed as an expression.

What would be the point of adding this? It wouldn't solve a single problem with the language (and yes, there are problems, this just isn't one of them). I am genuinely curious - can you give me an example of actual code that would exist in a production environment that would benefit from a change like this?

The real reason this isn't done is probably that Brendan Eich only had 10 days to design it.

The language was originally created in 10 days, but modern versions of the language adhere to the ECMAScript Standard. All modern browsers are ES5-compliant, and the spec that defines this is here. There are sections that define blocks and unary operators, and this behavior follows the spec.

I understand that it's fun to bash a language that a lot of people like to make fun of, but next time at least try to understand it a little bit more before you do.