Relational Expressions
This section describes JavaScript’s relational operators. These
operators test for a relationship (such as “equals,” “less than,” or
“property of”) between two values and return true or 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
if, while, and for statements (see Chapter 5). The subsections that follow document the
equality and inequality operators, the comparison operators, and
JavaScript’s other two relational operators, in and instanceof.
Equality and Inequality Operators
The == 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.
JavaScript supports =,
==, and === 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,” ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access