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. For relational abstract comparisons (e.g., <=
), the operands are first converted to primitives, then to the same type, before comparison.
Strings are compared based on standard lexicographical ordering, using Unicode values.
Features of comparisons:
true
or both are false
.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.
x == y
1 == 1 // true '1' == 1 // true 1 == '1' // true 0 == false // true 0 == null // false var object1 = {'value': 'key'}, object2 = {'value': 'key'}; object1 == object2 //false 0 == undefined // false null == undefined // true
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.
x != y
1 != 2 // true 1 != '1' // false 1 != "1" // false 1 != true // false 0 != false // false
The identity operator returns true if the operands are strictly equal (see above) with no type conversion.
x === y
3 === 3 // true 3 === '3' // false var object1 = {'value': 'key'}, object2 = {'value': 'key'}; object1 === object2 //false
The non-identity operator returns true if the operands are not equal and/or not of the same type.
x !== y
3 !== '3' // true 4 !== 3 // true
Each of these operators will call the valueOf()
function on each operand before a comparison is made.
The greater than operator returns true if the left operand is greater than the right operand.
x > y
4 > 3 // true
The greater than or equal operator returns true if the left operand is greater than or equal to the right operand.
x >= y
4 >= 3 // true 3 >= 3 // true
The less than operator returns true if the left operand is less than the right operand.
x < y
3 < 4 // true
The less than or equal operator returns true if the left operand is less than or equal to the right operand.
x <= y
3 <= 4 // true
The standard equality operators (==
and !=
) use the Abstract Equality Comparison Algorithm to compare two operands. If the operands are of different types, it will attempt to convert them to the same type before making the comparison, e.g., in the expression 5 == '5'
, the string on the right is converted to Number
before the comparison is made.
The strict equality operators (===
and !==
) use the Strict Equality Comparison Algorithm and are intended for performing equality comparisons on operands of the same type. If the operands are of different types, the result is always false
so 5 !== '5'
.
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 involved in the comparison (i.e., non–strict comparison), JavaScript converts the types String
, Number
, Boolean
, or Object
operands as follows:
Number
type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number
type value.Boolean
, the Boolean operand is converted to 1 if it is true
and +0 if it is false
.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.// true as both operands are type String (i.e. string primitives): 'foo' === 'foo' var a = new String('foo'); var b = new String('foo'); // false as a and b are type Object and reference different objects a == b // false as a and b are type Object and reference different objects a === b // true as a and 'foo' are of different type and, the Object (a) // is converted to String 'foo' before comparison a == 'foo'
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0 |
ECMAScript 3rd Edition (ECMA-262) | Standard | Adds === and !== operators. Implemented in JavaScript 1.3 |
ECMAScript 5.1 (ECMA-262) | Standard | Defined in several sections of the specification: Relational Operators, Equality Operators |
ECMAScript 2015 (6th Edition, ECMA-262) | Standard | Defined in several sections of the specification: Relational Operators, Equality Operators |
ECMAScript Latest Draft (ECMA-262) | Draft | Defined in several sections of the specification: Relational Operators, Equality Operators |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Equality (a == b ) |
Yes | Yes | Yes | Yes | Yes | Yes |
Inequality (a != b ) |
Yes | Yes | Yes | Yes | Yes | Yes |
Identity (a === b ) |
Yes | Yes | Yes | Yes | Yes | Yes |
Non-identity (a !== b ) |
Yes | Yes | Yes | Yes | Yes | Yes |
Greater than (a > b ) |
Yes | Yes | Yes | Yes | Yes | Yes |
Greater than or equal (a >= b ) |
Yes | Yes | Yes | Yes | Yes | Yes |
Less than (a < b ) |
Yes | Yes | Yes | Yes | Yes | Yes |
Less than or equal (a <= b ) |
Yes | Yes | Yes | Yes | Yes | Yes |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Equality (a == b ) |
Yes | Yes | Yes | Yes | Yes | Yes | ? |
Inequality (a != b ) |
Yes | Yes | Yes | Yes | Yes | Yes | ? |
Identity (a === b ) |
Yes | Yes | Yes | Yes | Yes | Yes | ? |
Non-identity (a !== b ) |
Yes | Yes | Yes | Yes | Yes | Yes | ? |
Greater than (a > b ) |
Yes | Yes | Yes | Yes | Yes | Yes | ? |
Greater than or equal (a >= b ) |
Yes | Yes | Yes | Yes | Yes | Yes | ? |
Less than (a < b ) |
Yes | Yes | Yes | Yes | Yes | Yes | ? |
Less than or equal (a <= b ) |
Yes | Yes | Yes | Yes | Yes | Yes | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators