start page | rating of books | rating of authors | reviews | copyrights

JavaScript: The Definitive Guide

Previous Chapter 4
Expressions and Operators
Next
 

4.4 Comparison Operators

This section describes the JavaScript comparison operators. These are operators that compare values of various types and return a boolean value (true or false) depending on the result of the comparison. As we'll see in Chapter 5, Statements, they are most commonly used in things like if statements and while loops to control the flow of program execution.

Equality (==)

The == operator returns true if its two operands are equal, and returns false if they are not equal. The operands may be of any type, and the definition of "equal" depends on the type.

In JavaScript, numbers, strings, and boolean values are compared by value. This means that two variables are equal only if they contain the same value. For example, two strings are equal only if they each contain exactly the same characters. In this case, there are two separate values involved, and the == operator checks that these two values are identical.

On the other hand, objects and arrays are compared by reference. This means that two variables are equal only if they refer to the same object. Two separate arrays will never be equal, by the definition of the == operator, even if they contain identical elements. For two variables that contain references to objects, arrays, or functions, they are equal only if they refer to the same object, array, or function. If you want to test that two separate objects contain the same properties or that two separate arrays contain the same elements, you'll have to check the properties and elements yourself. (And, if any of the properties or elements are themselves objects or arrays, you'll have to decide which kind of equality you want to test for.)

In Navigator 3.0, functions are compared by reference, just as objects and arrays are. Prior to 3.0, functions may not be used with the == operator.

Usually, if two values have different types, then they are not equal. Because JavaScript automatically converts data types when needed, though, this is not always the case. For example, the expression "1" == 1 evaluates to true in JavaScript. Similarly, and not surprisingly to C or C++ programmers, true == 1 and false == 0 are also both true expressions. In Navigator 2.0, null is equal to 0, but this was a bug was fixed in 3.0. Be careful when comparing values of different types: if you compare a string to a number, and the string cannot be converted to a number, then Navigator 2.0 and 3.0 will produce an error message. Internet Explorer takes the simpler, and probably correct, course and returns false in this case.[1]

[1] Navigator 4.0 behaves as Internet Explorer does when running JavaScript 1.2 code.

Note that the equality operator == is very different from the assignment operator =, although in English, we often read both as "equals". It is important to keep the two operators distinct and to use the correct one in the correct situation. To keep them straight, it may help to read the assignment operator = as "is assigned" or as "gets".

Inequality (!=)

The != operator tests for the exact opposite of the == operator. If two variables are equal to each other, then comparing them with the != operator will return false. On the other hand, comparing two objects that are not equal to each other with != will return true. As we'll see, the ! operator computes the boolean NOT operation. This makes it easy to remember that != stands for "not equal to."[2] See the discussion of the == operator for details on how equality is defined for different data types.

[2] There is one case in which the != operator is not the exact opposite of ==, when a != b is not identical to !(a == b). This occurs with the NaN value (Not-a-Number), which is never equal or inequal to itself. That is, if either operand is NaN, both == and != return false.

Less Than (<)

The < operator evaluates to true if its first operand is less than its second operand; otherwise it evaluates to false. The operands must be numbers or strings. Strings are ordered alphabetically, by character encoding.

Greater Than (>)

The > operator evaluates to true if its first operand is greater than its second operand; otherwise it evaluates to false. The operands must be numbers or strings. Strings are ordered alphabetically, by character encoding.

Less Than or Equal (<=)

The <= operator evaluates to true if its first operand is less than or equal to its second operand; otherwise it evaluates to false. The operands must be numbers or strings. Strings are ordered alphabetically, by character encoding.

Greater Than or Equal (>=)

The >= operator evaluates to true if its first operand is greater than or equal to its second operand; otherwise it evaluates to false. The operands must be numbers or strings. Strings are ordered alphabetically, by character encoding.


Previous Home Next
Arithmetic Operators Book Index String Operators

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell