I just ran into a problem where I did:
return
isSomething() &&
isSomethingElse();
Which doesn't work because JavaScript inserts the semicolon after the return making the above equivalent to:
return;
isSomething() && isSomethingElse();
This totally baffled me as to why it does this. I found some Stack Overflow questions about the topic (e.g. this, this, and this) but they just explain when it does this, referring to the specs.
I can't even imagine a situation where I would want to have a return;
statement followed by some other valid JavaScript statements (unless you use goto
or maybe some other obscure JavaScript I haven't heard of). In my opinion, this can only cause problems.
What I'm wondering is why it does this. Why is this part of the spec?
Concerning the close as duplicate. I think I clearly stated that I read other questions and answers stating that it's part of the JavaScript spec and even put the part that distinguishes my question from the others in bold. The question that is linked in the close reason does not contain an answer to this question and is of the exact same form as the other three questions I linked as not answering my question.
The exact reasons why are probably lost in the mists of time. I'm willing to bet that it happened something like this:
return
statements in the way you describe.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With