How to use method in JavaScript

The method was introduced in ES6 (ECMAScript 2015) that can be used to determine whether two JavaScript objects or primitive values are equal. is very similar to the JavaScript strict comparison operator ===, but there are some slight differences which we'll cover in this article.

You can use with numbers, strings, and even objects to test equality as shown below:'Mango', 'Mango');        // true'Mango', 'Orange');       // false, window);          // true, null);              // true[], []);                  // false

const animals = { penguin: '🐧' };
const fruits = { orange: '🍊' };, animals);        // true, fruits);         // false

// Special Cases, -0);                  // true, -0);                   // false, 0 / 0);              // true

The method always returns false unless one of the following conditions holds:

  • Both values are undefined
  • Both values are null
  • Both values are either true or false
  • Both values are strings of the same length with the same characters in the same order
  • Both values are the same exact object (refer to the same object)
  • Both values are numbers (and both +0, both -0, or both NaN)

JavaScript treats +0 and -0 differently, so be careful when you're comparing them using vs. === Operator

Although and strict comparison operator(===) can almost be used interchangeably, there are two main differences.

The first difference is that the === operator treats the number values -0 and +0 as equal which is not the case with

-0 === +0;              // true, +0);      // false

The second difference is that the === operator, unlike, treats Number.NaN as not equal to NaN:

Number.NaN === NaN;             // false, NaN);     // true

Browser Compatibility

The method only works in modern browsers. If you need to support old browsers like Internet Explorer, use the following polyfill provided by MDN:

if (! {
  Object.defineProperty(Object, "is", {
    value: function (x, y) {
      // SameValue algorithm
      if (x === y) { // Steps 1-5, 7-10
        // Steps 6.b-6.e: +0 != -0
        return x !== 0 || 1 / x === 1 / y;
      } else {
        // Step 6.a: NaN == NaN
        return x !== x && y !== y;

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.