How Object.is() method differ from strict equality (===) ?
Last Updated :
12 Feb, 2024
Object.is():
- The
Object.is()
method is used to compare two values for equality.
- It returns
true
if the values are the same, and false
otherwise.
- It handles special cases such as
-0
and NaN
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));
console.log(Object.is(1, "1" ));
console.log(Object.is(NaN, NaN));
console.log(Object.is(+0, -0));
|
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, and false
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);
console.log(1 === "1" );
console.log(NaN === NaN);
console.log(+0 === -0);
|
Output
true
false
false
true
Share your thoughts in the comments
Please Login to comment...