DEV Community

Discussion on: Semicolon Rules in JavaScript are weird...

Collapse
 
david_saint_ profile image
David Saint

The return statement is part of some javascript statements that must be terminated with semicolons and is therefore affected by automatic semicolon insertion (ASI) along with others like (continue, import, let, const e.t.c)

There are 3 rules for the ASI, but I think the one you're looking for is this

"A semicolon is inserted at the end when the end of the input stream of tokens is detected and the parser is unable to parse the single input stream as a complete program".

View the rest at developer.mozilla.org/en-US/docs/W...

Thread Thread
 
dean profile image
dean

Interesting! So since return is a keyword that must end with a semicolon, a semicolon is placed. But because return x ends with an identifier, it checks the next line to see if it is parsable as a program, and if it is, then it doesn't insert a semicolon. Neat!