The problem is the horror that is automatic semicolon insertion. JavaScript's rules for ASI will insert a ;
after the return
, giving you this:
return;
"some text";
That seems odd, but it's valid — it does a return
with no value, and so the function call returns undefined
, and then there's an expression lying around at the end of the function (which is valid, but does nothing).
You can fix it by doing what you did (putting it all on one line), which is what I'd recommend, or changing it so that the ASI rules won't kick in, for instance by using parens:
return (
"some text"
);
(I'm not advocating using parens, just saying it that will fix the problem.)
In a comment you've asked what other situations come up where a newline might trigger ASI when you don't want it to. They're fairly rare, newline after return
is by far the biggest one. Here's another example:
return
{
result: "success"
};
There the author meant to return an object, but instead fell prey to the same ASI error that hit your code.
The take-away message here is: If you're returning something, put the beginning of it on the same line as the return
. That wipes out the vast majority of the situations where ASI causes a problem.
Axel Rauschmayer has a good article about ASI and when/why it kicks in on his blog (which is worth following). The only non-return example from it is:
a
++
c
...which triggers ASI and becomes
a;
++
c
...but the original was pretty dodgy to begin with. :-)