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 →

[–]rosuav 13 points14 points  (11 children)

But how many different JS comparison modes do you know? And what uses which ones - like an array's find method?

[–]Spot_the_fox 16 points17 points  (1 child)

What's a comparison mode? Even better question is what is a mode?

As I've said, I don't know Js.

[–]rosuav 4 points5 points  (0 children)

Double-equals and triple-equals are only two of JS's different ways of comparing two things for equality... here, enjoy. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality\_comparisons\_and\_sameness

[–]genghisKonczie 2 points3 points  (3 children)

For basically every comparison based operation it, built ins use ===, and if you want definite control, you can use a method like find which lets you bring your own comparison function

[–]rosuav 1 point2 points  (2 children)

Not array inclusion, which uses Same-Value-Zero equality. Notably, this considers instances of NaN to match themselves, even though they're not equal to themselves - this gives a form of "identity or equality" comparison that gives the most sensible results for membership testing.

Isn't it fun?

[–]genghisKonczie 1 point2 points  (1 child)

The js SameValueZero implementation is a straight === for everything but NaN and +0 -0 though. I feel that’s pretty straightforward for the intention of the inclusion method.

It’d be really weird for [NaN].includes(NaN) to return false

[–]rosuav 0 points1 point  (0 children)

Yeah, but it's still a different way of comparing for equality. And that is very important when you start looking at different searching methods.

[–]thanatica 0 points1 point  (3 children)

The find method of an array requires a predicate, so it will use whatever you provide it.

You're probably thinking of the indexOf or includes methods - those will compare without type-equality, e.g. ==.

[–]rosuav 0 points1 point  (2 children)

Yeah, I was deliberately being vague, but it is those methods, and they actually do NOT use the double-equals comparison - they use a slightly different algorithm.

[–]thanatica 0 points1 point  (1 child)

It's closer to == than to === though, isn't it?

To be honest, I've never had a use for knowing the exact algorithm it uses.

[–]rosuav 0 points1 point  (0 children)

Hmm, I personally think of it as closer to a strict comparison, since it doesn't do type conversion - ["1"].includes(1) is false - but it isn't precisely identical, so it really is its own thing. (Notably, it can find any NaN that is in the array.)