# no-unsafe-negation

Deny the use of ! on the left hand side of an instanceof or in expression where it is ambiguous.

JavaScript precedence is higher for logical not than it is for in or instanceof. Oftentimes you see expressions such as !foo instanceof bar, which most of the times produces unexpected behavior. precedence will group the expressions like (!foo) instanceof bar. Most of the times the developer expects the expression to check if foo is not an instance of bar however.

# Incorrect Code Examples

if (!foo instanceof String) {
}
if (!bar in {}) {
}
More incorrect examples
!foo in bar
![5] instanceof !4
More correct examples

If this is intended behavior, you can wrap the expression

(!foo) instanceof bar
key in bar
bar instanceof bar

Source (opens new window)

Last Updated: 11/18/2020, 9:36:33 PM