Bugs in negated `instanceof` expressions

Author: gorosgobeCreated Sep 20, 2023Updated Jun 19, 2024

in and instanceof expressions in JS

javascript
a in obj;
a instanceof C;

can be negated by grouping them and applying the ! operator, i.e.

javascript
!(a in obj);
!(a instanceof C);

Applying the ! operator incorrectly (on the LHS operand) leads to bugs:

javascript
!a in obj; // will evaluate to false, unless obj has a "true" or "false" key
!a instanceof C; // will evaluate to false, unless C overrides instanceof with a @@hasInstance method

For more information, please see these MDN docs and the no-unsafe-negation recommended Eslint rule.

I have found a potentially problematic instance of the above bugs in your codebase: https://github.com/iissnan/hexo-theme-next/blob/9c8cea69bf0d4f91c07779d71b01814b27bbb6a1/source/lib/Han/dist/han.js#L2154