当值为布尔类型时使用!!(双重否定)语法是否属于冗余写法?
!!IS_DEV Redundant When IS_DEV is Already a Boolean? Great question! Let's break this down clearly:
First, let's confirm what !!IS_DEV does when IS_DEV is a strict boolean:
- The first
!inverts the boolean value:!truebecomesfalse,!falsebecomestrue - The second
!inverts it again:!falsebecomestrue,!truebecomesfalse
So when IS_DEV is guaranteed to be only true or false, !!IS_DEV will always evaluate to exactly the same value as IS_DEV itself. There's no functional difference between const sandboxMode = !!IS_DEV; and const sandboxMode = IS_DEV; here—no change to the value, type, or runtime behavior.
Is this a case of redundant syntax?
Yes, absolutely. Since IS_DEV is already a boolean, wrapping it in double negation doesn't serve any practical purpose—it's just unnecessary extra syntax that adds no value.
Why did ESLint fix it this way?
ESLint's rule that flagged your original ternary (IS_DEV ? true : false) is built to eliminate redundant boolean conversions. For cases where the input might be a truthy/falsy non-boolean value (like 0, "", or an object), !!value is a concise way to coerce it to a strict boolean. But in your specific scenario where IS_DEV is guaranteed to be a boolean, this fix introduces a new, unnecessary redundancy.
What's the best approach here?
If you're certain IS_DEV will always be a boolean (true or false), stick with const sandboxMode = IS_DEV;—it's cleaner, more readable, and avoids unnecessary runtime operations.
内容的提问来源于stack exchange,提问作者Luis Febro

