operators test for a relationship (such as “equals,” “less than,” or
“property of”) between two values and return
false depending on whether that relationship
exists. Relational expressions always evaluate to a boolean value, and
that value is often used to control the flow of program execution in
for statements (see Chapter 5). The subsections that follow document the
equality and inequality operators, the comparison operators, and
=== operators check whether two values are
the same, using two different definitions of sameness. Both
operators accept operands of any type, and both return
true if their operands are the same and
false if they are different. The
=== operator is known as the
strict equality operator (or sometimes the identity operator), and
it checks whether its two operands are “identical” using a strict
definition of sameness. The
operator is known as the equality operator; it checks whether its
two operands are “equal” using a more relaxed definition of sameness
that allows type conversions.
=== operators. Be sure you understand the
differences between these assignment, equality, and strict equality
operators, and be careful to use the correct one when coding!
Although it is tempting to read all three operators “equals,” it may
help to reduce confusion if you read “gets or is assigned” for
=, “is equal to” for
==, and “is strictly equal to” for
!== operators test for the exact opposite
=== operators. The
false if two
values are equal to each other according to
== and returns
true otherwise. The
!== operator returns
false if two values are strictly equal to
each other and returns
otherwise. As you’ll see in Logical Expressions, the
! operator computes the Boolean
NOT operation. This makes it easy to remember that
!== stand for “not equal to” and “not
strictly equal to.”
The strict equality operator
=== evaluates its operands, and then
compares the two values as follows, performing no type
If the two values have different types, they are not equal.
If both values are
nullor both values are
undefined, they are equal.
If both values are the boolean value
trueor both are the boolean value
false, they are equal.
If one or both values is
NaN, they are not equal. The
NaNvalue is never equal to any other value, including itself! To check whether a value
x !== x.
NaNis the only value of
xfor which this expression will be true.
If both values are numbers and have the same value, they are equal. If one value is
0and the other is
-0, they are also equal.
===or to the
Compare()in Part III for another way to compare strings.
If both values refer to the same object, array, or function, they are equal. If they refer to different objects they are not equal, even if both objects have identical properties.
The equality operator
like the strict equality operator, but it is less strict. If the
values of the two operands are not the same type, it attempts some
type conversions and tries the comparison again:
If the two values have the same type, test them for strict equality as described above. If they are strictly equal, they are equal. If they are not strictly equal, they are not equal.
If the two values do not have the same type, the
==operator may still consider them equal. Use the following rules and type conversions to check for equality:
If one value is
nulland the other is
undefined, they are equal.
If one value is a number and the other is a string, convert the string to a number and try the comparison again, using the converted value.
If either value is
true, convert it to 1 and try the comparison again. If either value is
false, convert it to 0 and try the comparison again.
If one value is an object and the other is a number or string, convert the object to a primitive using the algorithm described in Object to Primitive Conversions and try the comparison again. An object is converted to a primitive value by either its
toString()method or its
toString()conversion, except for the Date class, which performs
Any other combinations of values are not equal.
As an example of testing for equality, consider the comparison:
This expression evaluates to
true, indicating that these very
different-looking values are in fact equal. The boolean value
true is first converted to the
number 1, and the comparison is done again. Next, the string
"1" is converted to the number 1.
Since both values are now the same, the comparison returns
- Less than (
- Greater than (
- Less than or equal (
- Greater than or equal (
The operands of these comparison operators may be of any type. Comparison can be performed only on numbers and strings, however, so operands that are not numbers or strings are converted. Comparison and conversion occur as follows:
If either operand evaluates to an object, that object is converted to a primitive value as described at the end of Object to Primitive Conversions: if its
valueOf()method returns a primitive value, that value is used. Otherwise, the return value of its
toString()method is used.
If, after any required object-to-primitive conversion, both operands are strings, the two strings are compared, using alphabetical order, where “alphabetical order” is defined by the numerical order of the 16-bit Unicode values that make up the strings.
If, after object-to-primitive conversion, at least one operand is not a string, both operands are converted to numbers and compared numerically.
-0are considered equal.
Infinityis larger than any number other than itself, and
-Infinityis smaller than any number other than itself. If either operand is (or converts to)
NaN, then the comparison operator always returns
integer values, and that string comparison is just a numerical
comparison of the values in the two strings. The numerical encoding
order defined by Unicode may not match the traditional collation
order used in any particular language or locale. Note in particular
that string comparison is case-sensitive, and all capital ASCII
letters are “less than” all lowercase ASCII letters. This rule can
cause confusing results if you do not expect it. For example,
according to the
the string “Zoo” comes before the string “aardvark”.
For a more robust string-comparison algorithm, see the
which also takes locale-specific definitions of alphabetical order
into account. For case-insensitive comparisons, you must first
convert the strings to all lowercase or all uppercase using
+ operator and the
comparison operators behave differently for numeric and string
+ favors strings: it
performs concatenation if either operand is a string. The comparison
operators favor numbers and only perform string comparison if both
operands are strings:
// Addition. Result is 3.
// Concatenation. Result is "12".
// Concatenation. 2 is converted to "2". Result is "12".
// Numeric comparison. Result is false.
// String comparison. Result is true.
// Numeric comparison. "11" converted to 11. Result is false.
// Numeric comparison. "one" converted to NaN. Result is false.
Finally, note that the
<= (less than or equal) and
>= (greater than or equal) operators do
not rely on the equality or strict equality operators for
determining whether two values are “equal.” Instead, the
less-than-or-equal operator is simply defined as “not greater than,”
and the greater-than-or-equal operator is defined as “not less
than.” The one exception occurs when either operand is (or converts
NaN, in which case all four
comparison operators return
in operator expects a
left-side operand that is or can be converted to a string. It
expects a right-side operand that is an object. It evaluates to
true if the left-side value is
the name of a property of the right-side object. For
// Define an object
// => true: object has property named "x"
// => false: object has no "z" property.
// => true: object inherits toString method
// An array with elements 0, 1, and 2
// => true: array has an element "0"
// => true: numbers are converted to strings
// => false: no element 3
expects a left-side operand that is an object and a right-side
operand that identifies a class of objects. The operator evaluates
true if the left-side object
is an instance of the right-side class and evaluates to
objects are defined by the constructor function that initializes them. Thus,
the right-side operand of
instanceof should be a function. Here are
// Create a new object with the Date() constructor
// Evaluates to true; d was created with Date()
// Evaluates to true; all objects are instances of Object
// Evaluates to false; d is not a Number object
// Create an array with array literal syntax
// Evaluates to true; a is an array
// Evaluates to true; all arrays are objects
// Evaluates to false; arrays are not regular expressions
Note that all objects are instances of
instanceof considers the “superclasses”
when deciding whether an object is an instance of a class. If the
left-side operand of
is not an object,
false. If the right-hand
side is not a function, it throws a
In order to understand how the
instanceof operator works, you must
mechanism, and it is described in Inheritance. To
evaluate the expression
f.prototype, and then looks for that value
in the prototype chain of
it finds it, then o is an instance of f (or of a superclass of f)
and the operator returns
f.prototype is not one of the
values in the prototype chain of
o is not an instance of f and