Check out a free preview of the full Introduction to JavaScript course
The "Equality Comparisons" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson:
Brian demonstrates usage of the strict equality operator.
Transcript from the "Equality Comparisons" Lesson
[00:00:00]
>> Brian Holt: The next thing I'm gonna show you is you don't necessarily have to have a boolean inside of those if statements, right? So I'm gonna say if 2 + 2, and then I'm gonna put triple equals here, 4.
>> Brian Holt: So let's talk about triple equals here. Above here, if I say something like const myVariable = 10.
[00:00:25]
Notice I'm using one equal sign right here. Now, in your brain, when you are thinking about programing, I want you to rewrite this. Don't think of this as is equal to. Think of this as is assigned, right? My variable is assigned 10, right? So if you have one equal signed, that means it's assigned.
[00:00:46]
If you put triple equals here you are asking a question. Is 2 + 2 equal to 4? So this is actually being reduced down to a boolean of true or false, right. Is this equal to this. So here I can say console.log.
>> Brian Holt: Array [LAUGH] I think that the fundamental principles and mathematics still hold true.
[00:01:15]
That's what I have in my notes, but [LAUGH]
>> Brian Holt: Math still works.
>> Brian Holt: Else console.log("Uh, panic?").
>> Brian Holt: So you can get rid of that up at the top.
>> Brian Holt: So now if I save this and refresh over here you can say hurray if math still works, right?
>> Brian Holt: So why triple?
[00:01:59]
That's another question that people would typically ask. If you do double equals here,
>> Brian Holt: It still works. The reason why I'm imploring you to learn to do with triple equals is because double equals has some funny business that is does, that for the most part you don't want it to do.
[00:02:18]
There's a thing called coercion that's going to try and convert. For example, if I had here instead of four, this is the number four, if I had the string four here. Notice that this is between double quotes, so now that this has become a string, this will still work, but if I put triple equals here, it does not work.
[00:02:44]
When you put triple equals, it does not try and coerce one into the other. Now coercion in JavaScript is complicated business. We'll put it that way and it's not something we really want to get into today. Suffice to say, for the most part if you're doing coercion, usually you don't want to.
[00:03:01]
There's an entire Frontend Masters course on it from Kyle Simpson. If it's interesting to you definitely go check it out, but for now, just follow what I'm telling you and just stick with triple equals until you understand what exactly what's going on there. You'll save yourself a lot of heartache.
[00:03:20]
I promise.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops