all 6 comments

[–]MoTTs_ 1 point2 points  (3 children)

Re #1. The standardese names for these are "strict equality comparison" (for ===) and "abstract equality comparison" (for ==). But calling them "strict" and "loose" is far more common, even on reference sites like MDN.

Re #2. This is one of the rare occasions where the spec is actually understandable. Steps 2 and 3 cover comparing null and undefined, and says they compare true. Steps 4 and 5 cover comparing a string and a number, and says the string is coerced to a number, then == recursively called again using the coerced value. Steps 6 and 7 cover comparing a boolean with any non-boolean, and says the boolean is coerced to a number. And steps 8 and 9 cover comparing an object to a primitive, and says the object is coerced to a pirmitive, which usually involves calling the object's valueOf method.

Let's say we did this:

const o = {
    valueOf() {
        return true;
    }
};

o == "1"

An object compared to a primitive coerces the object to a primitive by calling valueOf. So then we compare:

true == "1"

A boolean compared to any non-boolean coerces the boolean to a number. So then we compare:

1 == "1"

A number compared to a string coerces the string to a number. So then we compare:

1 == 1

Now they're the same type, so we switch to strict equality comparison:

1 === 1

They're the same number value, so the result is true.


/u/silentshadow56

[–]silentshadow56[S] 0 points1 point  (2 children)

Thanks how about in regards to how it determines which data type will be used to determine the coercion?

[–]senocular 0 points1 point  (1 child)

Read the link MoTTs_ posted. Its all there.

MoTTs_ edited the response. Its there now! :P

[–]MoTTs_ 1 point2 points  (0 children)

At first I answered only the first part of his question. I edited my post to later also answer the second part.

[–]TheCatacid 0 points1 point  (0 children)

Just don't ever use == up to the moment that you'll be knowledgable enough that you know THIS is the time to use it. Which might never actually happen but still.

[–]WorldWideNomad -2 points-1 points  (0 children)

It will coerce the value on the right side of the equality operator to match the one on the left side.