Deep JavaScript Foundations, v3

Double Equals Algorithm

Kyle Simpson

Kyle Simpson

You Don't Know JS
Deep JavaScript Foundations, v3

Check out a free preview of the full Deep JavaScript Foundations, v3 course

The "Double Equals Algorithm" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle analyzes the spec on coercive equality to understand completely each step of the double equals comparison.

Preview
Close

Transcript from the "Double Equals Algorithm" Lesson

[00:00:00]
>> Kyle Simpson: If we're not talking about Knowles and undefines, but we're talking about strings, numbers and Booleans, there's a prevailing sense here, from clauses four through seven, that essentially, this algorithm prefers to do numeric comparison. You'll notice four different places it calls the two number. It says if the type X is number, but the other one is string, then call two number on the string, so I can do a numeric comparison.

[00:00:29]
Same thing in five, same thing in six. If one of them's Boolean, then do a two number on it, and make the comparison. So it prefers to reduce everything down to numbers to make the comparison. Now you may not like that that's the way the algorithm works, but just by knowing that it helps you to understand the behaviors and predict the behaviors of the double equals algorithm.

[00:00:52]
That in some cases you thought one thing was happening, but actually an entirely different thing was happening. Instead of two strings getting compared, it's two numbers getting compared. Just knowing in the back of your head double equals prefers numeric comparison. That one fact separates you from 99.99% of all JavaScript developers, cuz they've never even read the spec.

[00:01:13]
So how do we see that playing out? Well, if I have a string on a number, like I am on line one, that's a number and online too, it's a string. If I do a triple equals, of course, I'm gonna have to forcibly make the numbers to do a comparison, they're gonna have to be the same type.

[00:01:29]
If I had a triple equals and they were different types, it's always gonna fail. So I'm gonna have to make numbers. But if I know there can only be number or string, especially if one of them is definitely a number, then an exactly the same way as I allow this with the last things sign, several sides ago, why not also allow with the double equals, why not say that this code online for is more noisy unnecessarily so and the abstraction of the double equals it's helpful here.

[00:01:59]
It's saying whether they're a number or a string, let's let them compare to each other. And if they're equal, great, and if not, great. But use the abstraction of the tools.
>> Kyle Simpson: I can do this because I designed this particular function or this particular piece of code to restrict the types, to say that the only types here that can be considered for the equality comparison are the string and the number.

[00:02:30]
This would be a significantly more complex code if I would allow arrays and objects and bullions and all kinds of other types to come into that comparison. So the case that I keep making is, you can choose to structure your code in such a way that coercion is a useful and obvious system, rather than the complex magic that some people feel.

[00:02:50]
It brings it back within the realm of reasonable and practical for your code basis. Not just for you, but for all the members of your development.
>> Speaker 2: Look, numerical strengths in both cases, they could blow up, wouldn't they? It wasn't both numerical strengths like from forum.
>> Kyle Simpson: So if they were both strings, let's go back to thinking about the algorithm.

[00:03:10]
If both of them were strings, what does the algorithm do?
>> Speaker 2: Transmit the numbers.
>> Kyle Simpson: Nope, if they are both strings, that means they are both of the same type, then what does the algorithm do?
>> Speaker 3: It does the triple equals.
>> Kyle Simpson: It does the triple equals. So my response to you is, if those strings were literally identical, you'd get true, and otherwise you wouldn't.

[00:03:30]
But that's the same for every string that you do. You couldn't reasonably expect a string that said 3.7 and a string that said. 3.07 to equal each other, those strings are equal and those numbers learn.
>> Speaker 2: Like a form input that some outputs base in front of it, for example, they become weird but if it's a number about the space in front.

[00:03:50]
>> Kyle Simpson: Again, let's reset back to the context, the double equals is going to allow coercion when the types are different. If you're in a scenario where the types are not different, where the types are the same, then it's not ever going to invoke coercion, ever. If you want some other additional behavior, the thing that you want is not coercion here, the thing you want is string trimming, or string formatting or something like that.

[00:04:15]
If you have a case where you have two strings that could have whitespace on either side, and you really only care about the stuff in between. Great, there's a thing for that and it's called string trimming. But that isn't coercion. Coercion is when I have a string and a number, and I'd like those to be compared to each other.

[00:04:31]
Does that help?
>> Speaker 2: Yeah, yeah.
>> Kyle Simpson: Okay, all right. So back to our algorithm, the final part, classes 8 and 9, if you use a double equals with something that's not already a primitive, guess what we invoke our friend to primitive. We invoke the two primitive abstract operation.

[00:04:53]
We already looked at that. Remember that's the one that calls value over two string or whatever. The takeaway here is that double equals only compares primitives. If you use it with something that's not a primitive, what's it gonna do? Turn it into a primitive. The only time it does something with non primitives is when they're the exact same type and then it just does the same value triple equals comparison.

[00:05:19]
Otherwise, it says, listen, the only coercion step I care about right now is getting you to be a primitive. And then we'll reconsider whether the primitives are at the same type or not. Remember how I said earlier in the course, that these algorithms are sorta inherently recursive. The double equals operator says, I need to get it to the point, where one of these clauses matches.

[00:05:42]
If you give me some scenario where it doesn't match completely, then I'm gonna re-run the algorithm over and over and over again, until I get it down to two primitives that are of the same type. Or two primitives that can be coercively equal to each other. And if I never get to that point, guess what, false, they are not equal.

[00:06:00]
So in other words, we would invoke the two primitive and get two primitives, and then come back to the algorithm with our two primitives, and then make the decision accordingly.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now