1. What is the difference between Equality (==) and Identity (===)?
2. Is there a performance benefit with Equality (==) or Identity (===)?
Answers:
1. What is the Difference Between Equality (==) And Identity (===)?
Equality (==) returns true if both value/operands have the same value, without worrying about the type of both value/operands.
e.g.
e.g.
Identity (===) does not do type conversion (if types are not the same it returns false), So Identity (===) is faster than Equality (==).
1. What is the Difference Between Equality (==) And Identity (===)?
Equality (==) returns true if both value/operands have the same value, without worrying about the type of both value/operands.
e.g.
- 2 == 1 // returns false, both value/operands have different value.
- 2 == 2 // returns true, both value/operands have same type and value
- 2 == "2" // returns true, while both value/operands have different type, first value is type of number and second one is type of string, but value is same.
e.g.
- 2 === 1 // returns false, both value/operands have different value.
- 2 === 2 // returns true, both value/operands have same type and value
- 2 === "2" // returns false, both value/operands have same value but different types, first value is type of number and second one is string.
Identity (===) does not do type conversion (if types are not the same it returns false), So Identity (===) is faster than Equality (==).
NOTE: Identity (===), is generally better and safer because there is no behind the scenes type conversion.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.