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.

Object.is()

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:

  1. both are undefined
  2. both are null
  3. both are true or both false
  4. both strings are of the same length with the same characters in the same order
  5. both are the same object (which means both objects have the same reference)
  6. both numbers are and
  7. both 0
  8. both -0
  9. both NaN
  10. 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. 🙂