Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
What are the differences between ===
vs ==
and !==
vs !=
?
When should you use each one?
The strict equality operator ( === ) behaves identically to the abstract equality operator ( == ) except no type conversion is done, and the types must be the same to be considered equal. The == operator will compare for equality after doing any necessary type conversions.
== check if the one side is equal to the second one, while the === check if the left side is equal to the second one but from the same type. check if both are the same string and both are string values. Note also there is the !== operator that it does negative value and type comparison.
The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
The strict inequality operator ( !== ) checks whether its two operands are not equal, returning a Boolean result. Unlike the inequality operator, the strict inequality operator always considers operands of different types to be different.
=== is the Identity operator, and is used to test that value and type are equal.
so..
"3" == 3 // true "3" === 3 // false 1 == true // true 1 === true // false "1" == true // true "1" === true // false
so when you care that value and type are equal, or not equal use Identity operators === or !==
The "normal" ==
operators in javascript perform type coercion, and try their best to do things like treat a string as number or an object as a string where required. The longer === operators will not do type coercion, but rather a strict comparison within the type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With