Check out a free preview of the full Introduction to JavaScript course
The "Abstract Comparison" 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 covers usage of the double equals comparison operator in JavaScript.
Transcript from the "Abstract Comparison" Lesson
[00:00:00]
>> Brian: So one last tidbit here.
>> Brian: Where was this? Here, notice that I'm using == here when I just told you, I don't know, an hour ago, not to do ==. When you're doing the math here, you should use ==. And why. So userAnswer is coming from here, from this input here in the browser.
[00:00:32]
If I type 15 in here, the browser doesn't know that's a number. The browser assumes everything is a string, right? So here, I'm asking you to do math, number times number, that's going to give you back a number, but userAnswer is always gonna be a string. But we can check if they're equivalent by doing double equals cuz it's gonna force those two things to both be numbers.
[00:00:58]
That makes sense? So when you're doing math, use double equals. Everything else, you can use triple equals, if that's too much of a difficulty just use double equals for the rest of this quiz, just not afterwards [LAUGH]. Yeah. Questions.
>> Speaker 2: Is that the case for everything that would ever be typed into a browser?
[00:01:16]
Like, if you're working with a form, everything you get from that form will be a string. And you have to either turn it into a number.
>> Brian: Yep.
>> Speaker 2: Or do something like that.
>> Brian: Yes if you want to do it a different way and not use double equals here, you could also say const newAnswer, whatever you want to call it, equals parseInt(userAnswer).
[00:01:40]
ParseInt is given to you by the browser to be able to turn strings into numbers. And then here you would be able to use triple equals. I'm fine with either way that you choose to do that.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops