Contents

About type conversion

   Jul 19, 2023     1 min read

This article has been about type conversion

At my company, we had a situation where we had to format our code because eslint gave us some strong conditions. Among them, I encountered the sentence ā€œRedundant double negation.eslintno-extra-boolean-castā€ the most.

Letā€™s see what that error is, why it pops up, and what the solution is.

What is the ā€œRedundant double negation.eslintno-extra-boolean-castā€ error?

When using eslint, the ā€œRedundant double negation.eslintno-extra-boolean-castā€ error that you may see is often caused by the error is known as the ā€œno-extra-boolean-castā€ rule. This rule detects unnecessary double negation operations and displays a warning.

if (!!value) {
  // ...
}

This is what you see in code like this. I knew what it was.

Why does it come up?

As you can see in the example above, ā€œ!!valueā€ forces a negative operation on the boolean value ā€˜valueā€™, then repeats it twice by performing the negative operation again. While this is used as a form of casts and makes sense to us, it is unnecessary for the formatting recommended by eslint, and we believe it makes the code more complex and less readable.

Now that we know why weā€™re seeing that, letā€™s see how we can fix it.

Solution

Since the above phrase is unnecessary as described, ESLINT recommends removing it. It should look like this

if (value) {
  // ...
}

The modified code above uses ā€˜valueā€™ as it is to evaluate the condition. This eliminates unnecessary negative operations and makes the code more concise.

Reference

When using eslint to statically analyze code, the ā€œRedundant double negationā€ rule is applied to detect similar unnecessary negative operations. By applying this rule, you can make your code more readable and avoid potential bugs.