Why is JavaScript designed to automatically insert a semicolon after a return, followed by a new line?

I just ran into a problem where I did this:

return
    isSomething() &&
    isSomethingElse();

which does not work because javascript inserts a semicolon after returning, making the above equivalent:

return;
isSomething() && isSomethingElse();

This completely puzzled me about why he was doing this. I found some StackOverflow questions on the topic (like this , this and this ), but they just explain when this is done, referring to the specifications .

, return;, javascript ( goto , , - javascript, ). , .

, . ?

: , . , , , , JavaScript, , . , , , , , .

+4
3

, , , . , :

  • - - , .
  • - , return , .
  • return, , , .
+4

Javascript "" , , , , . , . :

The semicolon is only obligatory when you have two or more statements on the same line:

var i = 0; i++        // <-- semicolon obligatory
                      //     (but optional before newline)
var i = 0             // <-- semicolon optional
    i++               // <-- semicolon optional

, , , Javascript , , , .

+1

"", .

Javascript .

-2
source

Source: https://habr.com/ru/post/1540455/


All Articles