JavaScript has both strict and type–converting comparisons. A strict comparison (e.g., ===
) is only true if the operands are of the same type and the contents match. The more commonly-used abstract comparison (e.g. ==
) converts the operands to the same type before making the comparison.
Equality (==)
The equality operator converts the operands if they are not of the same type, then applies strict comparison. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
1 2 3 4 5 6 7 8 9 |
<span class="token number">1</span> <span class="token operator">==</span> <span class="token number">1</span> <span class="token comment" spellcheck="true">// true</span> <span class="token string">'1'</span> <span class="token operator">==</span> <span class="token number">1</span> <span class="token comment" spellcheck="true">// true</span> <span class="token number">1</span> <span class="token operator">==</span> <span class="token string">'1'</span> <span class="token comment" spellcheck="true">// true</span> <span class="token number">0</span> <span class="token operator">==</span> <span class="token keyword">false</span> <span class="token comment" spellcheck="true">// true</span> <span class="token number">0</span> <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token comment" spellcheck="true">// false</span> <span class="token keyword">var</span> object1 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'value'</span><span class="token punctuation">:</span> <span class="token string">'key'</span><span class="token punctuation">}</span><span class="token punctuation">,</span> object2 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'value'</span><span class="token punctuation">:</span> <span class="token string">'key'</span><span class="token punctuation">}</span><span class="token punctuation">;</span> object1 <span class="token operator">==</span> object2 <span class="token comment" spellcheck="true">//false</span> <span class="token number">0</span> <span class="token operator">==</span> undefined <span class="token comment" spellcheck="true">// false</span> <span class="token keyword">null</span> <span class="token operator">==</span> undefined <span class="token comment" spellcheck="true">// true</span> |
Inequality (!=)
The inequality operator 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.
Identity / strict equality (===)
The identity operator returns true if the operands are strictly equal (see above) with no type conversion.
1 2 3 4 |
<span class="token number">3</span> <span class="token operator">===</span> <span class="token number">3</span> <span class="token comment" spellcheck="true">// true</span> <span class="token number">3</span> <span class="token operator">===</span> <span class="token string">'3'</span> <span class="token comment" spellcheck="true">// false</span> <span class="token keyword">var</span> object1 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'value'</span><span class="token punctuation">:</span> <span class="token string">'key'</span><span class="token punctuation">}</span><span class="token punctuation">,</span> object2 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'value'</span><span class="token punctuation">:</span> <span class="token string">'key'</span><span class="token punctuation">}</span><span class="token punctuation">;</span> object1 <span class="token operator">===</span> object2 <span class="token comment" spellcheck="true">//false</span> |
Non-identity / strict inequality (!==)
The non-identity operator returns true if the operands are not equal and/or not of the same type.
1 2 |
<span class="token number">3</span> <span class="token operator">!==</span> <span class="token string">'3'</span> <span class="token comment" spellcheck="true">// true</span> <span class="token number">4</span> <span class="token operator">!==</span> <span class="token number">3</span> <span class="token comment" spellcheck="true">// true</span> |