JavaScript Object is() Method Examples

Example: In this example, Object.is() returns true for the first comparison because both values are 5, but false for the second comparison because 5 and '5' are of different types. It returns true for NaN comparison because NaN is considered the same value as itself, and false for 0 and -0 comparison because they are considered different values.

Javascript
console.log(Object.is(5, 5)); // true
console.log(Object.is(5, '5')); // false
console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(0, -0)); // false

Output
true
false
true
false

JavaScript Object is() Method

JavaScript Object.is() method is used to compare if two values are the same value.

Object.is() returns true if the values are the same, and false otherwise. It differs from the strict equality operator === in the handling of NaN and positive/negative zero.

Syntax:

Object.is(value1, value2)

Parameters:

Object.is() Method takes two parameters:

  • value1: It is the first value to be compared. 
  • value2: It is the second value to be compared.

Return Value:

Object.is() returns a boolean indicating whether the two arguments are the same or not. 

Similar Reads

JavaScript Object is() Method Examples

Example: In this example, Object.is() returns true for the first comparison because both values are 5, but false for the second comparison because 5 and '5' are of different types. It returns true for NaN comparison because NaN is considered the same value as itself, and false for 0 and -0 comparison because they are considered different values....

Exceptions

The “==” and “===” operator treats the number values “+0” and “-0” as equal but the object.is() method treats them differently.The Object.is() method does not coerce values before comparison even if they are of different data types....