question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Examples of correct code for no-unneeded-ternary

See original GitHub issue

This is a discussion/question.

Why are

var a = x ? y : x;

var a = x ? x : 1;

considered correct?

cf http://eslint.org/docs/rules/no-unneeded-ternary#rule-details

You could write them as

var a = x && y;

var a = x || 1;

What am I missing?

This rule disallow ternary operators when simpler alternatives exist.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Mar 7, 2017

I agree, it would be nice if the rule caught these.

Other cases:

!x ? y : x;
!x ? x : y;

// gets fixed to

x || y;
x && y;

I think the rule should still report MemberExpressions, even if it doesn’t autofix them. Getters with side-effects are very rare, so the report would still be useful in the vast majority of cases.

0reactions
Mouvediacommented, Oct 4, 2018

Seems like an oversight to me: should be fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No-unneeded-ternary - ESLint - W3cubDocs
This rule disallow ternary operators when simpler alternatives exist. Examples of incorrect code for this rule: /*eslint no-unneeded-ternary: "error"*/var a ...
Read more >
ESLint error no-unneeded-ternary - javascript - Stack Overflow
ESLint is telling me this error message inside my JS module: error no-unneeded-ternary Unnecessary use of conditional expression for default ...
Read more >
Comparison Operators & Equality · Styleguide JavaScript
Examples. ⇣ Incorrect code for this rule: ... Examples. An array (even an empty one) is an object and Objects will evaluate ......
Read more >
ESLint - no-ternary Disallows ternary operators. - Runebook.dev
Examples of incorrect code for this rule: /*eslint no-ternary: "error"*/ var foo = isBar ? baz : qux; function quux() { return foo...
Read more >
disallow ternary operators (no-ternary) - Breword 文档集合
The ternary operator is used to conditionally assign a value to a variable. Some believe that the use of ternary operators leads to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found