Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ternary operators, again #89

Open
lxg opened this issue Aug 21, 2016 · 2 comments
Open

Ternary operators, again #89

lxg opened this issue Aug 21, 2016 · 2 comments

Comments

@lxg
Copy link

lxg commented Aug 21, 2016

I agree that multi-line ternary operators make sense. However, the following is (as far as I know) bad, due to ASI:

var foo = (a === b)
    ? 1
    : 2;

JShint will throw an error on this code, saying “Bad line breaking before '?'”.

If you insist on using multi-line ternary operators, you might prefer the following style:

var foo = (a === b) ?
    1 :
    2;

I agree that it is less readable, because the question mark and the colon are more obvious at the beginning of the line. But I wouldn’t want to mess with ASI and depend on implicit fixes of the JS engine, either.

@rumkin
Copy link

rumkin commented Dec 7, 2016

@lxg I thinks this is an issue for JSHint. Line should starts with the heaviest syntax construction.

@lxg
Copy link
Author

lxg commented Dec 7, 2016

@rumkin this is not an issue of JSHint but rather of JS specs vs. what browsers allow. If browsers would strictly follow the specs in this case and apply ASI, the ternary operator in the first example would cause a syntax error at the second line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants