Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upTernary return expression #517
Comments
This comment has been minimized.
This comment has been minimized.
|
I think it's very scary to have the |
This comment has been minimized.
This comment has been minimized.
|
Oh good, I'm not the only one concerned about Does anyone have a pointer to the exact rules for |
This comment has been minimized.
This comment has been minimized.
|
If you're using Dead code is not allowed after
I actually think the fear around return
5gets interpreted as: return;
5;But this would also be wrong if you use semicolons! return
5;also gets interpreted as: return;
5;Takeaway: You still need to understand automatic semicolon insertion even if you always use semicolons in your code. Semicolons won't save you. The rule to remember about automatic semicolon insertion is: If inserting a semicolon at the end of the line would be syntactically valid, then that's what the JS engine will do. Hence why this works just fine: return condition
? 6000
: 1000Putting a semicolon after return condition; // <-- this doesn't make sense
? 6000
: 1000So it doesn't get inserted. (Thanks for indulging my rant ;) |
This comment has been minimized.
This comment has been minimized.
|
Thanks! .. although
would also be valid, no? Inserting a semi after the None the less, its great to have such a clear explanation .. I'll close the issue. |
backspaces
closed this
May 13, 2016
This comment has been minimized.
This comment has been minimized.
joeysino
commented
May 17, 2016
•
|
Yes it's valid Javascript. It's just not standard-style. ;) I think it's a bit like the discussion around leading or trailing commas. You can either signal continuation at the end of the line, or you can signal continuation at the beginning of the line. But you can't do both! Let's just take a look at the two cases:
or:
I feel the second one looks tidier and clearer than the first one. The leading symbol indicates whether the following value will be the if-true or if-false value. |
backspaces commentedMay 11, 2016
•
edited
I ran into a problem with ternary return functions. I got an eslint warning when I used this form:
Standard warned: ? should be placed at the beginning of the line, i.e.
But for all us no-semis folks, we know that
returnis odd beyond belief! I wanted the ? on the return line to flag that there is more to the return expression. I guess I could put the expression in parens?The second form does work, but I don't know why, given return's oddnesses.
I realize Standard is rigid on purpose, but I'd like the first form to be OK, mainly for return statements. My guess is eslint itself makes this difficult? Maybe I should get over
returnfear .. is there a good description somewhere for what the return syntax rules are?