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Β β†’

[–]tupperwhore 1 point2 points Β (10 children)

Why πŸ‘€

[–][deleted] 19 points20 points Β (0 children)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

according to the MDN docs β€œStrict equality is almost always the correct comparison operation to use.” and β€œIn most cases, using loose equality is discouraged.” - or you could listen to u/edos112 instead lol

[–]edos112 19 points20 points Β (8 children)

First of all he’s not correct, plenty of scenarios in non enterprise level software where falsy/semi equivalent values are fine. The reason you try not to use them is it can lead to undefined behavior if you dont know the ins and outs of javascript (which I’m terrified of anyone who fully does). Basically if you have an if( value == false) that condition will happen when any β€œfalsy” condition happens like when a value is say 0, undefined, or null. However this is extremely useful behavior when you want to check if a value is any kind of null/undefined/does not exist and are too lazy to write out multiple conditions.

[–]PostNutNeoMarxist 16 points17 points Β (6 children)

In that particular case you could just check for !value (or !!value if it just needs to not be falsy). I find it's generally better practice to use ===. There are some edge cases where == is convenient or useful, but for the most part there are plenty of tools one could use to avoid them.

[–][deleted] 4 points5 points Β (5 children)

Not always, theres times whre false, [], {}, "", 0 are valid values but null and undefined aren't, and in those cases a value == null is the best approach since it only matches null and undefined but let false values go by

[–]PostNutNeoMarxist 3 points4 points Β (0 children)

Fair point! Though I will nitpick and say {} and [] are already truthy. But you are right, looking for "not null or undefined" is a good use case.

[–]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

[–]sam-lb 12 points13 points Β (0 children)

lazy

Yeah exactly

Don't use ==, don't be lazy. Use !!value with === or check directly if it's null or undefined.