+ 1
Are == and === used for the same purpose?
I got this question today in a challenge. In my opinion the answer is yes (quiz answer is no), because both are used for the purpose of comparing. sure they are not equal as === also compares types. in oposition == and = for example are not used for the same purpose as one is for compairing and one for asigning. what are your thoughts on this?
7 Réponses
+ 11
Julian Konrath
== compares only value but === compares both the value and type.
10 == "10" is true
10 === "10" is false since data types are different
https://www.sololearn.com/Discuss/1010908/?ref=app
+ 14
"==" checks only for equality in value whereas "===" is a stricter equality test and returns false if either the value or the type of the two variables are different.
So, the second option needs both the value and the type to be the same for the operands.
console.log( false == '0' ); // true
console.log( false === '0' ); // false
+ 11
Julian Konrath if it's about quiz question then please attach the complete question what it says. Then we can give any answer and you can report the question if you think it's wrong after completion go to view correct answer and press too right yellow (i) mark then choose the option accordingly
+ 3
Well I think your idea about same purpose is not clear to everyone. Yes both those operators check for equality but === is stricter and as you already said, = is used for a different purpose of assignment.
+ 1
GAWEN STEASY
thanks for the reply. i'm aware of the difference as i wrote in the post.
still the purpose of both operators is to compare.
like var and let have the same purpose in my eyes, namely to declare a variable, even though they do it with different scopes.
+ 1
GAWEN STEASY
Yes, it's about the quiz question. I think the formulation is too vague.
The topic of this chat already is the complete question. Here again: "Are == and === used for the same purpose?"
The question should be "Are == and === doing the same thing?"
or "Are == and === interchangeable?.
As purpose is a more general term, I assign it to the category of operators. Like in the following table.
operator purpose
== compare (value)
=== compare (value + type)
<, >, !=, >=, <= compare
= assign
let declare
var declare
for loop
while loop
So e.g. the purpose of _let_ is to declare a variable, the purpose of _for_ is to create a loop.
let and for have a different purpose.
The purpose of >= is to compare the content of two variables. <= does the same.
<= and => have the same purpose.
Hope I'm making myself clearer (:
0
Sonic
You're definitely right about that. That's why i keep getting an answer that is already stated in the question.
To say == and === differ in strictness and share the same purpose sounds like good wording to me.