What is the use of the Object.is() method in JavaScript ?
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)
returnstrue
because both values are numerically equal. - In the second example,
Object.is(5, "5")
returnsfalse
because the values are of different types (number and string). - In the third example,
Object.is(NaN, NaN)
returnstrue
because both values are NaN. - In the fourth example,
Object.is(+0, -0)
returnsfalse
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
Contact Us