JavaScript(JS) object method - is
The Object.is()
method is a built-in method of the JavaScript Object
constructor. It compares two values and returns a boolean indicating whether they are the same value.
Here's the syntax:
Object.is(value1, value2)
where value1
and value2
are the values to be compared. This method takes two arguments, which are the values to be compared.
The Object.is()
method is similar to the ===
operator, but it has a few differences. Here are some of the key differences:
Object.is()
treatsNaN
as equal to itself, whereas===
does not.Object.is()
treats-0
and+0
as not equal, whereas===
treats them as equal.Object.is()
treats+0
and-0
as not equal to any other number, whereas===
treats them as equal to each other and to any other positive or negative zero.
Here's an example that shows how to use the Object.is()
method:
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 console.log(Object.is(+0, 0)); // true
In this example, we compare various values using the Object.is()
method. The first comparison returns true
because the two values are equal. The second comparison returns false
because the two values are not of the same type. The third comparison returns true
because both NaN
values are considered equal by Object.is()
. The fourth comparison returns false
because +0
and -0
are considered not equal by Object.is()
. The fifth comparison returns true
because +0
and 0
are considered equal by Object.is()
.