+ 4
What is difference of == and ===?
I'm pretty expert on JavaScript, but still I haven't understood what's the difference from == to === in conditions. Can someone explain me please?
6 Answers
+ 7
==is used to only compare values on the other hand
=== is also used to compare data types
đđ»
+ 6
console.log("100" == 100, "100" === 100, 100 === 100)
// true false true
== compare only value
=== compares value & type data
+ 5
== checks if the value is equals so 4 == "4" is true because one side is converted to the other type (in JavaScript this is done by Abstract Equality Comparison Algorithm). This leads also to weird results like NaN == NaN is false.
=== checks also if the type is the same so 4 === 4 is true and 4 === "4" is false.
The same rules are there for
!= and !==.
There are languages who know === like JavaScript and PHP but the most languages don't know ===
+ 4
== just compares values while === strictly compare values with types including....
"1" == 1 returns true
"1" === 1 returns false
+ 1
== is used to compare values, === is used to compare values and their data types.
0
== compares only values.
=== compares values and types in same time
For exemple:
('33' == 33 ) is true
But
('33' === 33) is false cuz '33' is a string but 33 is a number