What is the use of the Object.is() method in JavaScript ?
Last Updated :
12 Feb, 2024
The Object.is()
method in JavaScript serves the purpose of comparing two values for equality. It returns a boolean value indicating whether the two values are the same.
Syntax:
Object.is(value1, value2)
Parameter:
value1
: The first value to compare.
value2
: The second value to compare.
Example: Here,
- In the first example,
Object.is(5, 5)
returns true
because both values are numerically equal.
- In the second example,
Object.is(5, "5")
returns false
because the values are of different types (number and string).
- In the third example,
Object.is(NaN, NaN)
returns true
because both values are NaN.
- In the fourth example,
Object.is(+0, -0)
returns false
because the two zero values have different signs.
Javascript
console.log(Object.is(5, 5));
console.log(Object.is(5, "5" ));
console.log(Object.is(NaN, NaN));
console.log(Object.is(+0, -0));
|
Output
true
false
true
false
Share your thoughts in the comments
Please Login to comment...