JS_SameValue

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_SameValue
  • Revision title: JS_SameValue
  • Revision id: 326429
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{ jsapi_minversion_header("1.8.1") }}

Determines if two jsvals are the same, as determined by the SameValue algorithm in ECMAScript 262, 5th ed.  SameValue slightly differs from strict equality (===) in that +0 and -0 are not the same and in that NaN is the same as NaN.  The SameValue algorithm is equivalent to the following JavaScript:

function SameValue(v1, v2)
{
  if (v1 === 0 && v2 === 0)
    return 1 / v1 === 1 / v2;
  if (v1 !== v1 && v2 !== v2)
    return true;
  return v1 === v2;
}

Syntax

JSBool JS_SameValue(JSContext *cx, jsval v1, jsval v2);
Name Type Description
cx JSContext * Pointer to a JS context from which to derive runtime information. {{ Jsapi-requires-request() }}
v1 jsval The first value.
v2 jsval The second value.

Description

JS_SameValue determines whether two values are the same, returning JS_TRUE or JS_FALSE accordingly.  This method is infallible.

{{ LXRSearch("ident", "i", "JS_SameValue") }}

Revision Source

<p>{{ jsapi_minversion_header("1.8.1") }}</p>
<p>Determines if two <code>jsval</code>s are the same, as determined by the SameValue algorithm in ECMAScript 262, 5th ed.&nbsp; SameValue slightly differs from strict equality (<code>===</code>) in that <code>+0</code> and <code>-0</code> are not the same and in that <code>NaN</code> is the same as <code>NaN</code>.&nbsp; The SameValue algorithm is equivalent to the following JavaScript:</p>
<pre>
function SameValue(v1, v2)
{
  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="Syntax" name="Syntax">Syntax</h2>
<pre class="eval">
<a href="/En/SpiderMonkey/JSAPI_Reference/JSBool" title="en/JSBool">JSBool</a> <strong>JS_SameValue</strong>(<a href="/en/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/JSRuntime">JSContext</a> *cx, <a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a> v1, <a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a> v2);
</pre>
<table class="fullwidth-table">
  <tbody>
    <tr>
      <th>Name</th>
      <th>Type</th>
      <th>Description</th>
    </tr>
    <tr>
      <td><code>cx</code></td>
      <td><code><a href="/en/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/JSRuntime">JSContext</a>&nbsp;*</code></td>
      <td>Pointer to a JS context from which to derive runtime information. {{ Jsapi-requires-request() }}</td>
    </tr>
    <tr>
      <td><code>v1</code></td>
      <td><code><a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a></code></td>
      <td>The first value.</td>
    </tr>
    <tr>
      <td><code>v2</code></td>
      <td><code><a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a></code></td>
      <td>The second value.</td>
    </tr>
  </tbody>
</table>
<h2 id="Description" name="Description">Description</h2>
<p><code>JS_SameValue</code> determines whether two values are the same, returning <code>JS_TRUE</code> or <code>JS_FALSE</code>&nbsp;accordingly.&nbsp; This method is infallible.</p>
<p>{{ LXRSearch("ident", "i", "JS_SameValue") }}</p>
Revert to this revision