Object.is()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/is
  • Revision title: Object.is
  • Revision id: 372709
  • Created:
  • Creator: Waldo
  • Is current revision? No
  • Comment

Revision Content

{{harmony}}
{{fx_minversion_header("22")}}

Summary

Determines whether the passed two values are really same.

Method of Object
Implemented in JavaScript 1.8.5+
ECMAScript Edition ECMAScript 6th Edition

Syntax

Object.is(value1, value2)

Parameters

value1
The first value to compare.
value2
The second value to be compare.

Description

Object.is() is almost the same as the === operator. The only behavior differences are for Number values, in which case:

  • The two values are considered to be the same if both have the value NaN.
  • The two values are considered to be different if one has the value -0 and the other has the value +0.

Compatibility

Object.is is a proposed addition to the ECMA-262 standard; as such it may not be present in all browsers. You can work around this by utilizing the following code at the beginning of your scripts. This will allow you to use Object.is when there is still no native support.

if (!Object.is) {
  Object.is = function(value1, value2) {
    if (v1 === 0 && v2 === 0)
      return 1 / v1 === 1 / v2;
    if (v1 !== v1 && v2 !== v2)
      return true;
    return v1 === v2;
  };
}

Examples

Object.is("foo", "foo");     // true
Object.is(window, window);   // true

Object.is("foo", "bar");     // false
Object.is([], []);           // false

var test = {a: 1};
Object.is(test, test)       // true

Object.is(null, null)       // true

// Special Cases
Object.is(0, -0);            // false
Object.is(-0, -0);           // true
Object.is(NaN, 0/0);         // true

 

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatNo}}

{{CompatGeckoDesktop("22")}}

{{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatGeckoDesktop("22")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

 

Revision Source

<div>
  {{harmony}}</div>
<div>
  {{fx_minversion_header("22")}}</div>
<h2 id="Summary">Summary</h2>
<p>Determines whether the passed two values are really same.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of<code> <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object">Object</a></code></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8.5+</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 6th Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>Object.is(value1, value2)</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <em><code>value1</code></em></dt>
  <dd>
    The first value to compare.</dd>
  <dt>
    <em><code>value2</code></em></dt>
  <dd>
    The second value to be compare.</dd>
</dl>
<h2 id="Description">Description</h2>
<p><span class="short_text" id="result_box" lang="en"><span><code>Object.is()</code> is almost the same as the === operator. The only behavior differences are for <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number" title="/en-US/docs/JavaScript/Reference/Global_Objects/Number"><code>Number</code></a> values, in which case:</span></span></p>
<ul>
  <li><span class="short_text" lang="en"><span>The two values are <strong>considered to be the same</strong> if both have the value <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/NaN" title="/en-US/docs/JavaScript/Reference/Global_Objects/Number/NaN"><code>NaN</code></a>.</span></span></li>
  <li>The two values are <strong>considered to be different</strong> if one has the value -0 and the other has the value +0.</li>
</ul>
<h2 id="Compatibility" name="Compatibility">Compatibility</h2>
<p><code>Object.is</code>&nbsp;is a proposed addition to the ECMA-262 standard; as such it may not be present in all browsers. You can work around this by utilizing the following code at the beginning of your scripts. This will allow you to use <code>Object.is</code>&nbsp;when there is still no native support.</p>
<pre class="brush: js">
if (!Object.is) {
  Object.is = function(value1, value2) {
    if (v1 === 0 &amp;&amp; v2 === 0)
      return 1 / v1 === 1 / v2;
    if (v1 !== v1 &amp;&amp; v2 !== v2)
      return true;
    return v1 === v2;
  };
}
</pre>
<h2 id="Examples">Examples</h2>
<pre class="brush:js;">
Object.is("foo", "foo");     // true
Object.is(window, window);   // true

Object.is("foo", "bar");     // false
Object.is([], []);           // false

var test = {a: 1};
Object.is(test, test)       // true

Object.is(null, null)       // true

// Special Cases
Object.is(0, -0);            // false
Object.is(-0, -0);           // true
Object.is(NaN, 0/0);         // true</pre>
<p>&nbsp;</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatNo}}</td>
        <td>
          <p>{{CompatGeckoDesktop("22")}}</p>
        </td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatGeckoDesktop("22")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision