Andy
Andy

Reputation: 11472

sonarlint JavaScript warning: "Remove the unnecessary boolean literal"

Please note: as far as I'm aware this isn't a duplicate. There is some discussion about this warning in other languages but as we all know, == in JavaScript isn't like other languages.

The code reads:

let channelValue = filter == true ? 2 : 1;

SonarLint suggests "remove the unnecessary boolean literal, which tempts me to write:

let channelValue = filter ? 2 : 1;

but of course that will give a different result if filter has the value 87 or "Cheese on Toast". Is there a way to avoid the == true but retain the same semantics?

Upvotes: 2

Views: 3907

Answers (1)

Kiril
Kiril

Reputation: 301

You should consider using === instead of == as the second one is not recommended and may behave unexpectedly. It also resolves sonarlint warning. So you code should look like this:

let channelValue = filter === true ? 2 : 1;

Upvotes: 4

Related Questions