Open In App

How Object.is() method differ from strict equality (===) ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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)); // 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, 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); // true
console.log(1 === "1"); // false
console.log(NaN === NaN); // false
console.log(+0 === -0); // true


Output

true
false
false
true

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads