How Object.is() method differ from strict equality (===) ?
Object.is():
- The
Object.is()
method is used to compare two values for equality. - It returns
true
if the values are the same, andfalse
otherwise. - It handles special cases such as
-0
andNaN
differently compared to strict equality (===
). - Specifically,
Object.is()
distinguishes between +0 and -0, and between different NaN values.
Example: Below is an example of the object.is() method.
Javascript
console.log(Object.is(1, 1)); // true console.log(Object.is(1, "1" )); // false console.log(Object.is(NaN, NaN)); // true console.log(Object.is(+0, -0)); // false |
Output
true false true false
Strict Equality (===
):
- The strict equality operator (
===
) compares two values to check if they are equal. - It returns
true
if the values are the same and of the same type, andfalse
otherwise. - It does not differentiate between different types of NaN or between +0 and -0.
Example: Below is an example of strict equality(===).
Javascript
console.log(1 === 1); // true console.log(1 === "1" ); // false console.log(NaN === NaN); // false console.log(+0 === -0); // true |
Output
true false false true