Lesson 16
Strict Comparison Not Equal (!==) in JavaScript
Summary
Summary of JavaScript Strict Comparisons
In this transcript, we cover the differences between loose and strict equality comparisons in JavaScript.
Key Points
- Loose Equality (
==
and!=
):- Using
==
(double equals) checks for value equality, but it does not take types into account, which can lead to unexpected results. For example:3 == '3'
results intrue
(loose comparison).3 != '3'
results infalse
(loose comparison).
- Using
- Strict Equality (
===
and!==
):- Using
===
(triple equals) checks both the value and the type, giving a stricter comparison:3 === '3'
results infalse
(strict comparison).
- Thus, it is a best practice to use
===
and!==
for comparisons to avoid type-related bugs.
- Using
Best Practices
- Always use triple equals (
===
) and triple not equals (!==
) when comparing values in JavaScript to ensure type safety. - Be aware that while there may be rare exceptions where loose equality can be used, it is generally safer to stick with strict comparisons.
Additional Resources
- For more details, the transcript references the MDN documentation which provides examples and further explanations about strict comparisons in JavaScript.
Conclusion
Using strict equality operators helps create more predictable and error-free code. Always consult documentation for clarity and examples when needed.
Thank you for reading!
Video Transcript
Now, if you want to do the opposite, which is asserting something is not equal to, you
could use the exclamation point with the equal sign, right?
Remember that.
So for example, if you have three not equals to the string as a three, well, if you think
about in terms of types, this is a number, this is a string, you would think it would
be not equal, right?
But actually it's going to say what?
False.
Okay, this guy is equal to that.
So it's kind of like not really strict.
So three is not equal to the string three, and it's giving false.
But if you want to be really strict, these guys are not the same because even though
they have the same value, this guy has the type number and this guy has the type string.
So technically this should be true, right?
Because they actually don't have the same type, even though they have the same value.
So you can add one more equals and it's going to be true.
So three is not equal, strictly equal to the string three, that's true.
Okay, so that's strict comparison JavaScript.
So typically as you're developing in the real world, you want to use always, whenever you
can always start off with the triple equals when you're making comparisons that will make
you your code like less prone to bugs because you know exactly what kind of types you expect.
It's not like you expect a string or you expect a number and you don't know which one is
coming through.
So always make sure to start as a best good practice to start with the triple equals when
you want to make a comparison and or the not the exclamation point with the two equal signs
to make not equal to strict comparison.
So that's a good practice.
I want you to remember.
And yes, there might be cases, very few exceptions where you can use this guy and this guy, but
keep those special cases, but always make sure to start off with the triple comparison
operators.
Okay, so that is it.
Just reviewing what we did.
We learned that JavaScript is a bit loose when it comes to comparison.
If you use the triple, if you use the double equal or the exclamation point equal to compare
things to say, are they, is this the same as that?
Is this different from that?
Then it's kind of a bit loose when it comes to comparing numbers with numbers inside strings,
right?
As we saw before.
So to make that a little bit less stricter, right, we only use the strict comparison operators
add one more equal sign there, one more equal sign there, and that will also will make the
comparison be not only about the value, but also about the type of the things you're comparing.
Okay, if you want to have more reference on this, as we mentioned before, there's always
the good documentation out there online.
So let's minimize this.
Now do a search here for JavaScript strict comparisons.
For example, let's see if we get anything.
We got this MDN page.
So you can see strict comparison triple equals is only true if the upper ends are of the
same type and the contents match.
So that's exactly what we talked about.
Get some examples here.
So if you ever forget what we talked about, just come here and have a look at the examples
to see how these guys do.
You can run it.
You can see the output here so on, right?
And there's some details here.
And each operator equality, inequality.
We did that strict equality.
That's what we talked about in this lesson.
And you can see returns to the upper end are strictly equal with no type conversion.
That's the key here.
We will not try to convert that like for example, this number inside a string to an actual number
and then it will turn out to be true if it were only two equals.
You can see here no identity, a strict inequality.
We talked about that too.
Okay.
You can see an example here.
We did.
And so on.
Okay, so keep that in mind.
Always reference these wonderful documentation.
Thank you so much for watching and until the next time.
Bye.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: