Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The identity (<code>===</code>) operator behaves identically to the equality (<code>==</code>) operator except no type conversion is done, and the types must be the same to be considered equal.</p> <p>Reference: <a href="http://www.c-point.com/javascript_tutorial/jsgrpComparison.htm" rel="noreferrer">Javascript Tutorial: Comparison Operators</a></p> <p>The <code>==</code> operator will compare for equality <em>after doing any necessary type conversions</em>. The <code>===</code> operator will <strong>not</strong> do the conversion, so if two values are not the same type <code>===</code> will simply return <code>false</code>. Both are equally quick.</p> <p>To quote Douglas Crockford's excellent <a href="https://rads.stackoverflow.com/amzn/click/0596517742" rel="noreferrer">JavaScript: The Good Parts</a>,</p> <blockquote> <p>JavaScript has two sets of equality operators: <code>===</code> and <code>!==</code>, and their evil twins <code>==</code> and <code>!=</code>. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then <code>===</code> produces <code>true</code> and <code>!==</code> produces <code>false</code>. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:</p> <pre><code>'' == '0' // false 0 == '' // true 0 == '0' // true false == 'false' // false false == '0' // true false == undefined // false false == null // false null == undefined // true ' \t\r\n ' == 0 // true </code></pre> <p>The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use <code>===</code> and <code>!==</code>. All of the comparisons just shown produce <code>false</code> with the <code>===</code> operator.</p> </blockquote> <hr> <h3>Update:</h3> <p>A good point was brought up by <a href="https://stackoverflow.com/users/165495/casebash">@Casebash</a> in the comments and in <a href="https://stackoverflow.com/users/113570/philippe-leybaert">@Phillipe Laybaert's</a> <a href="https://stackoverflow.com/a/957602/1288">answer</a> concerning reference types. For reference types <code>==</code> and <code>===</code> act consistently with one another (except in a special case).</p> <pre><code>var a = [1,2,3]; var b = [1,2,3]; var c = { x: 1, y: 2 }; var d = { x: 1, y: 2 }; var e = "text"; var f = "te" + "xt"; a == b // false a === b // false c == d // false c === d // false e == f // true e === f // true </code></pre> <p>The special case is when you compare a literal with an object that evaluates to the same literal, due to its <code>toString</code> or <code>valueOf</code> method. For example, consider the comparison of a string literal with a string object created by the <code>String</code> constructor.</p> <pre><code>"abc" == new String("abc") // true "abc" === new String("abc") // false </code></pre> <p>Here the <code>==</code> operator is checking the values of the two objects and returning <code>true</code>, but the <code>===</code> is seeing that they're not the same type and returning <code>false</code>. Which one is correct? That really depends on what you're trying to compare. My advice is to bypass the question entirely and just don't use the <code>String</code> constructor to create string objects.</p> <p><strong>Reference</strong><br> <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3" rel="noreferrer">http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload