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 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. 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:, value2); 
//, 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 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 Method

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

// Case 2: Signed zero, -0);                 // false, -0);                // false, -0);                // true, -0n);               // true

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


if (! {
  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;