Lesson 15
Strict Comparison Equal (===) in JavaScript
Summary
Strict Comparison in JavaScript
In this lesson from NVK Tech World, we explore the concept of strict comparison in JavaScript, focusing on the triple equals (===
) operator and its distinction from the double equals (==
) operator.
Key Concepts
-
Double Equals (
==
):- Used for comparing values, but it performs type coercion.
- Example:
2 == 2
results intrue
.3 == "3"
also results intrue
despite one being a number and the other a string.
-
Triple Equals (
===
):- Used for strict comparison, meaning it checks both the value and the type of the operands.
- Example:
3 === "3"
results infalse
due to different types (number vs. string).
- Ensures that comparisons between different types (like strings and numbers) do not lead to unexpected results.
Practical Use
When developing software, it's crucial to ensure that you're comparing values of the same type to avoid bugs. The triple equals operator helps maintain type integrity, avoiding mistakes that may arise from JavaScript's type coercion.
Type Checking
You can use the typeof
operator to check the type of a variable:
- For example:
typeof "3"
results in"string"
.typeof 3
results in"number"
.
By using strict comparison and type checking, developers can ensure their code behaves as expected.
Video Transcript
Welcome to NVK Tech World. In this lesson we'll be talking about strict
comparison in JavaScript. We'll learn about the triple equals operator and its
counterpart. As we saw before we can make comparison in JavaScript using the
double equal operator in this way. For example if you want to assert that 2 is
actually equal to 2 you can use the double equals. So 2 is obviously equal
to true therefore the result here which is the boolean value is true. Now if you
have 3 and you try to do 3 equal to that's false right? So that out sounds
great there is no problem there but then what happens if you try to compare a
string that contains a number and an actual number that's where the problem
lies. Things get a bit less strict in JavaScript when you do this comparison.
For example let's see if the number 3 is actually is equal to the 3 as a
string which is not really a number this guy's a string because there are
quotes around it. So you're going to get true even though technically this number
is not really a string so that's not really a strict comparison in JavaScript.
So because of this there is also what's called a triple operator to account for
this kind of problem because sometimes usually you want to make sure that you
are dealing with the correct types you want to make sure you only have a
number when you need a number and you don't want to be mixing numbers with
strings in the manner that it might cause a lot of bugs as you're developing
your software. So you want to make sure you know exactly what you're working
with and am I working with strings am I working in numbers that kind of stuff so
that's why we have the triple equals operator. This guy will make a strict
comparison it will not tolerate you trying to compare a number inside a
string with an actual number. So let's see what we get if we try to compare a
string 3 let me put the quotes here with the number 3 you're going to get
false because it's a strict operation. This guy here the triple equals will not
only check for the value that it thinks the types have in this case they both
have value kind of 3 right but the type is different so the triple equals will
also make sure to check that the type of each of these operands is the same.
Okay also check for the type and let's see the type of each of these guys.
Remember we can use that operator called type of to see what the type or
something is. Type of this 3 here is a string but the type of the other 3 is a
number so they're obviously different types so the triple equals will make sure
okay we have kind of the same value but you do not have the same type therefore
this is false this is a strict comparison so that's the triple equals okay.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: