JavaScript How Object.is() Method Works
Hello devs, today I'll show you how to compare two variables with JavaScript built-in method called Object.is(). It is introduced in ES2015. Let's see the how we can use it in our application.
JavaScript Object is() a built-in function that checks whether two values are the same. Two values are the same if one of the following holds:
- both are undefined
- both are null
- both are true or both false
- both strings are of the same length with the same characters in the same order
- both are the same object (which means both objects have the same reference)
- both numbers are and
- both 0
- both -0
- both NaN
- or both non-zero and both not NaN, and both have the same value
It accepts two parameters and returns a Boolean value. It means either true of false. Let's look at the code snippet below
let a = 'JS'
let b = 'JS'
console.log(Object.is(a, b)); //true
And this will produce the below result
That's it for today. I hope it'll be helpful. Thanks for reading. 🙂