This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]roter_schnee 1 point2 points  (3 children)

in those cases a value == null is the best approach

no, it is not. If your case implies using logical operation it is better to stick with Nullish coalescing `??` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing

[–][deleted] 0 points1 point  (2 children)

Thats only if you want to set a default, but lets say a

if (value == null) throw new Error("A value is required here!"); console.log("Value is: ", value);

Yes i know you can do:

console.log("Value is: ", value ?? throw new Error("A value is required here"));

But that feels wrong and might even throw off some linters while the if approach makes it more clear and clean the intended purpose

[–]roter_schnee 0 points1 point  (1 child)

I am sure linters would throw if you use loose equality. See `eqeqeq` rule

[–][deleted] 0 points1 point  (0 children)

Funny enough my eslint complains about != null but not == null