0
votes

I want to set the variable notEmpty to true or false with a ternary operator, but my VSCode sonarlint marks it underlined in blue with the comment: Simplify this expression

Code: const notEmpty = list.length > 0 ? true : false;

This actually works, but could be better.

2
const notEmpty = list.length > 0 - Nick
I recommend reading up on booleans some more - But I'm Not A Wrapper Class
Nice @Nick , that works. Thanks. - Manuel Tapia

2 Answers

0
votes

try this:

const notEmpty = list.length ? true : false;

no need to compare it to 0 as it returns 0 when it's empty which is false, otherwise it's true

0
votes

You can also do this:

const notEmpty = Boolean(list.length);

Converting the value of 0 to false, or 1 to true

As mentioned in the comments above this is equivalent to:

const notEmpty = !!list.length;