The Object is() method | JavaScript Object is() method Syntax, Description, Parameters & Examples

Javascript Object is() Method with Example is discussed here elaborately for better understanding to beginners and experienced programmers. Want to gain more knowledge about the javascript object.is() method? Look at the below sections by using the direct links available here.

JavaScript Object is() Method

This method was introduced in ES2015. Mainly, it intends to help to compare values. TheObject.is()method is used to decide whether two values are the same or not. Two values can be the same if they hold one of the following properties:

  • In case both the values are undefined.
  • If both the values are null.
  • In case both the values are true or false.
  • If both the strings are of the same length with the same characters and in the same order.
  • In case both the values are numbers and both are “+0”.
  • If both the values are numbers and both are “-0”.
  • In case both the values are numbers and both are “NaN” or both non-zero and both not NaN and both have the same value.

Syntax of Object is() Javascript Method:

Object.is(value1, value2); 
//Object.is(a, b);

The result is always false unless:

  • a and b are the same exact object
  • a and b are equal strings (strings are equal when composed by the same characters, in the same order)
  • a and b are equal numbers (numbers are equal when their value is equal)
  • a and b are both undefined, both null, both NaN, both true or both false

0 and -0 are different values in JavaScript, so pay attention in this special case (convert all to +0 using the + unary operator before comparing, for example).

Also Check:

Parameters Used

  • value1: The first value to compare.
  • value2: The second value to compare.

Return Value

Javascript Object.is() method takes two arguments which are the values to be compared and declares a boolean indicating whether the two arguments are the same or not.

Browser Support

Chrome 30
Edge Yes
Firefox 22
Opera Yes

Example Using Object.is() Method

// Case 1: Evaluation result is the same as using ===
Object.is(25, 25);                // true
Object.is('foo', 'foo');          // true
Object.is('foo', 'bar');          // false
Object.is(null, null);            // true
Object.is(undefined, undefined);  // true
Object.is(window, window);        // true
Object.is([], []);                // false
var foo = { a: 1 };
var bar = { a: 1 };
Object.is(foo, foo);              // true
Object.is(foo, bar);              // false

// Case 2: Signed zero
Object.is(0, -0);                 // false
Object.is(+0, -0);                // false
Object.is(-0, -0);                // true
Object.is(0n, -0n);               // true

// Case 3: NaN
Object.is(NaN, 0/0);              // true
Object.is(NaN, Number.NaN)        // true

Polyfill

if (!Object.is) {
  Object.defineProperty(Object, "is", {
    value: function (x, y) {
      // SameValue algorithm
      if (x === y) {
        // return true if x and y are not 0, OR
        // if x and y are both 0 of the same sign.
        // This checks for cases 1 and 2 above.
        return x !== 0 || 1 / x === 1 / y;
      } else {
        // return true if both x AND y evaluate to NaN.
        // The only possibility for a variable to not be strictly equal to itself
        // is when that variable evaluates to NaN (example: Number.NaN, 0/0, NaN).
        // This checks for case 3.
        return x !== x && y !== y;
      }
    }
  });
}