Skip to content
Advertisements

Object.is()

Why Object.is ?

Object.is () checks whether the two values ​​are of the same value. The two values ​​are the same when one of the following is true:

  • when they both have the same object
  • both when and when
  • when both are +0
  • when both are -0
  • both right and wrong
  • two characters of the same length, when they are the same character in the same order
  • when they are both NaN
  • when both are undefined
  • when both are empty
  • or both are non-zero and not both NaN and both have the same value.
  • This is not the same as the == operator. == The operator applies the same difficulty to both sides before testing the equation (if not the same type) (but “==”), but Object.is does not force both values.

This is not the same as being equal to === operator. The operator === (and == operator) accepts -0 and +0 are equal and treats Number.NaN as not equal to NaN.

Syntax:

Object.is(arr1, arr2);

  • arr1: The first value to compare.
  • arr2: The second value to compare.

arr1arr2=====Object.is
undefined undefined true true true
nullnulltruetrue true
truetrue true true true
falsefalsetrue true true
“foo”“foo”truetruetrue
{ foo: “bar” }xtruetruetrue
00truetruetrue
+0-0truetruefalse
0falsetruefalsefalse
“”0truefalsefalse
“0”0truefalsefalse
“17”17truefalsefalse
[1,2]“1,2”truefalsefalse
new String(“foo”)“foo”truefalsefalse
nullundefinedtruefalsefalse
nullfalsefalsefalsefalse
undefinedfalsefalsefalsefalse
new String(“foo”) new String(“foo”) falsefalsefalse
0nullfalsefalsefalse
0NaNfalsefalsefalse
“foo”NaNfalsefalsefalse
NaNNaNfalsefalsetrue
{ foo: “bar” } { foo: “bar” } falsefalsefalse

JavaScript Object.is Example


        var test = { a: 1 };
        console.log( Object.is(test, test) )

        console.log( Object.is(null, null) )
        console.log( Object.is(0, -0) )
        console.log( Object.is(-0, -0) )
        console.log(Object.is(NaN, 0 / 0))

        Object.is('foo', 'foo');
        console.log(Object.is(window, window));

        Object.is('foo', 'bar');
        console.log(Object.is([], []));

output:

true
true
false
true
true
true
false


Polyfill

if (!Object.is) {
  Object.is = 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;
    }
  };
}

Browser Support

Chrome30
Edgeyes
Firefox22
Internet Explorerno
Operayes
Safari9
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android22
Opera Androidyes
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: