JavaScript Object is() Method

The Object is() method is used to check if two values are the same.

This method provides functionality similar to the strict equality (===) operator, but there are some differences particularly with NaN and +0/- 0.

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 are 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
    1. both +0
    2. both -0
    3. both NaN
    4. both non-zero and both not NaN, and both have the same value

Syntax

Object.is(value1, value2);

Parameters

  1. value1(required): The first value to compare.
  2. value2(required): The second value to compare.

Return value

Returns a boolean value indicating whether the two arguments are same or not.

Visual Representation

Visual Representation of JavaScript Object is() Method

Example 1: How to Use Object.is() Method

let a = 'AppDividend';
let b = 'AppDividend';
let c = 'Appdividend';

console.log(Object.is(a, b))
console.log(Object.is(a, c))

Output

true
false

Example 2: Zero and NaN comparison

console.log(Object.is(0, -0));
console.log(Object.is(+0, -0));
console.log(Object.is(+0, 0));
console.log(Object.is(-0, -0));

console.log(Object.is(NaN, 0/0));
console.log(Object.is(NaN, NaN));

Output

false
false
true
true

true
true

Example 3: Comparing empty arrays and objectsVisual Representation of Comparing empty arrays and objects

let arrA = [];
let arrB = [];

console.log(Object.is(arrA, arrB));

let objA = {};
let objB = {};

console.log(Object.is(objA, objB));

Output

false
false

Browser Compatibility

  • Chrome 19 and above
  • Edge12 and above
  • Firefox 22 and above
  • Opera 15 and above
  • Safari 9 and above

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.