Comparison Operators

  • Revision slug: JavaScript/Reference/Operators/Comparison_Operators
  • Revision title: Comparison Operators
  • Revision id: 50308
  • Created:
  • Creator: Stalve
  • Is current revision? No
  • Comment 48 words added

Revision Content

Summary

The operands can be numerical or string values. Strings are compared based on standard lexicographical ordering, using Unicode values.

JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
  • Two Boolean operands are strictly equal if both are true or both are false.
  • Two objects are strictly equal if they refer to the same Object.
  • Null and Undefined types are == (but not ===).

The following table describes the comparison operators:

Operator Description Examples returning true1
Equal (==) If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

3 == var1
"3" == var1
3 == '3'

Not equal (!=) Returns true if the operands are not equal. If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison. If both operands are objects, then JavaScript compares internal references which are not equal when operands refer to different objects in memory.

var1 != 4
var1 != "5"

Strict equal (===) Returns true if the operands are strictly equal (see above) with no type conversion.

3 === var1

Strict not equal (!==) Returns true if the operands are not equal and/or not of the same type.

var2 !== 3
3 !== '3'

Greater than (>) Returns true if the left operand is greater than the right operand.

var2 > var1

Greater than or equal (>=) Returns true if the left operand is greater than or equal to the right operand.

var2 >= var1
var1 >= 3

Less than (<) Returns true if the left operand is less than the right operand.

var1 < var2

Less than or equal to (<=) Returns true if the left operand is less than or equal to the right operand.

var1 <= var2
var2 <= 5

1 These examples assume that var1 has been assigned the value 3 and var2 has been assigned the value 4.

Using the Equality Operators

The standard equality operators (== and !=) compare two operands without regard to their type. The strict equality operators (=== and !==) perform equality comparisons on operands of the same type. Use strict equality operators if the operands must be of a specific type as well as value or if the exact type of the operands is important. Otherwise, use the standard equality operators, which allow you to compare the identity of two operands even if they are not of the same type.

When type conversion is needed, JavaScript converts String, Number, Boolean, or Object operands as follows.

  • When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number type value.
  • If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.
  • If an object is compared with a number or string, JavaScript attempts to return the default value for the object. Operators attempt to convert the object to a primitive value, a String or Number value, using the valueOf and toString methods of the objects. If this attempt to convert the object fails, a runtime error is generated.

{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_de_comparaison", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_por\u00f3wnania" } ) }}

Revision Source

<h2 name="Summary">Summary</h2>
<p>The operands can be numerical or string values. Strings are compared based on standard lexicographical ordering, using Unicode values.</p>
<p>JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:</p>
<ul> <li>Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.</li> <li>Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.</li> <li>Two Boolean operands are strictly equal if both are <code>true</code> or both are <code>false</code>.</li> <li>Two objects are strictly equal if they refer to the same Object.</li> <li>Null and Undefined types are <code>==</code> (but not <code>===</code>).</li>
</ul>
<p>The following table describes the comparison operators:</p>
<table class="fullwidth-table"> <tbody> <tr> <th>Operator</th> <th>Description</th> <th>Examples returning true<sup>1</sup></th> </tr> <tr> <td>Equal (<code>==</code>)</td> <td>If the two operands are <strong>not of the same type</strong>, JavaScript converts the operands then applies strict comparison. If either operand is <strong>a number or a boolean</strong>, the operands are converted to numbers if possible; else if either operand is<strong> a string</strong>, the other operand is converted to a string if possible. If both operands are <strong>objects</strong>, then JavaScript compares internal references which are equal when operands refer to the same object in memory.</td> <td> <p><code>3 == var1<br> "3" == var1<br> 3 == '3'<br> </code></p> </td> </tr> <tr> <td>Not equal (<code>!=</code>)</td> <td>Returns true if the operands are not equal. If the two operands are <strong>not of the same type</strong>, JavaScript attempts to convert the operands to an appropriate type for the comparison. If both operands are <strong>objects</strong>, then JavaScript compares internal references which are not equal when operands refer to different objects in memory.</td> <td> <p><code>var1 != 4<br> var1 != "5"</code></p> </td> </tr> <tr> <td>Strict equal (<code>===</code>)</td> <td>Returns true if the operands are strictly equal (see above) with no type conversion.</td> <td> <p><code>3 === var1</code></p> </td> </tr> <tr> <td>Strict not equal (<code>!==</code>)</td> <td>Returns true if the operands are not equal and/or not of the same type.</td> <td> <p><code>var2 !== 3<br> 3 !== '3'</code></p> </td> </tr> <tr> <td>Greater than (<code>&gt;</code>)</td> <td>Returns true if the left operand is greater than the right operand.</td> <td> <p><code>var2 &gt; var1</code></p> </td> </tr> <tr> <td>Greater than or equal (<code>&gt;=</code>)</td> <td>Returns true if the left operand is greater than or equal to the right operand.</td> <td> <p><code>var2 &gt;= var1<br> var1 &gt;= 3</code></p> </td> </tr> <tr> <td>Less than (<code>&lt;</code>)</td> <td>Returns true if the left operand is less than the right operand.</td> <td> <p><code>var1 &lt; var2</code></p> </td> </tr> <tr> <td>Less than or equal to (<code>&lt;=</code>)</td> <td>Returns true if the left operand is less than or equal to the right operand.</td> <td> <p><code>var1 &lt;= var2<br> var2 &lt;= 5</code></p> </td> </tr> </tbody>
</table>
<p><small><sup>1</sup> These examples assume that <code>var1</code> has been assigned the value 3 and <code>var2</code> has been assigned the value 4.</small></p>
<h2 name="Using_the_Equality_Operators">Using the Equality Operators</h2>
<p>The standard equality operators (<code>==</code> and <code>!=</code>) compare two operands without regard to their type. The strict equality operators (<code>===</code> and <code>!==</code>) perform equality comparisons on operands of the same type. Use strict equality operators if the operands must be of a specific type as well as value or if the exact type of the operands is important. Otherwise, use the standard equality operators, which allow you to compare the identity of two operands even if they are not of the same type.</p>
<p>When type conversion is needed, JavaScript converts String, Number, Boolean, or Object operands as follows.</p>
<ul> <li>When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a <code>Number</code> type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest <code>Number</code> type value.</li> <li>If one of the operands is <code>Boolean</code>, the Boolean operand is converted to 1 if it is <code>true</code> and +0 if it is <code>false</code>.</li> <li>If an object is compared with a number or string, JavaScript attempts to return the default value for the object. Operators attempt to convert the object to a primitive value, a <code>String</code> or <code>Number</code> value, using the <code>valueOf</code> and <code>toString</code> methods of the objects. If this attempt to convert the object fails, a runtime error is generated.</li>
</ul>
<p>{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_de_comparaison", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_por\u00f3wnania" } ) }}</p>
Revert to this revision