Skip to main content

Conditional Operators

The action part of an if-statement can include any of the JavaScript statements you've already learned (and any others, for that matter), but the condition part of the statement uses its own syntax. This is called a conditional expression.

A conditional expression usually includes two values to be compared (in the preceding example, the values were a and 1). These values can be variables, constants, or even expressions in themselves.

Between the two values to be compared is a conditional operator. This operator tells JavaScript how to compare the two values. For instance, the == operator is used to test whether the two values are equal. A variety of conditional operators is available:

Operator Description
== Is equal to
!= Is not equal to
< Is less than
> Is greater than
>= Is greater than or equal to
<= Is less than or equal to

Do not confuse the equality operator (==) with the assignment operator (=), even though they both might be read as "equals." Remember to use = when assigning a value to a variable, and == when comparing values.

Confusing these two is one of the most common mistakes in JavaScript programming.

Next: The Switch Statement