Object.is() in JavaScript
Object.is()
is a method in JavaScript that compares two values for equality. It returns a boolean value indicating whether the two values are the same or not.
The syntax for Object.is()
is as follows:
Object.is(value1, value2)
Here, value1
and value2
are the two values that are being compared.
Object.is()
has some differences from the ===
operator. The ===
operator compares two values for equality, but it has some quirks when it comes to certain types of values, such as NaN
and -0
. Object.is()
is designed to handle these quirks and provide a more accurate comparison.
Here are some examples of using Object.is()
:
Object.is(5, 5); // true
Object.is(5, "5"); // false
Object.is(NaN, NaN); // true
Object.is(0, -0); // false
Object.is(-0, -0); // true
In the first example, Object.is()
returns true
because both values are the same.
In the second example, Object.is()
returns false
because the two values are not the same. Even though the string "5" can be converted to the number 5, they are not the same value.
In the third example, Object.is()
returns true
because NaN
is considered to be the same as NaN
.
In the fourth example, Object.is()
returns false
because 0
and -0
are considered to be different values.
In the fifth example, Object.is()
returns true
because -0
is considered to be the same as -0
.