HTML and CSS Reference

In-Depth Information

Table 5.6
Comparison Operators (continued)

Operator/Operands

Function

x >= y

x
is greater than or equal to
y

x < y

x
is less than
y

x <= y

x
is less than or equal to
y

x = = = y

x
is identical to
y
in value and type

x != = y

x
is not identical to
y

What Is Equal?
In an id l wo ld, th e would be equ y b tw en the ex s and

among the races and religions, but in the real world equality is a debatable topic, often

determined by governments. In JavaScript, operators determine the equality or inequal-

ity of their operands, based on more specific rules. When using the == or != equality

operators, the operands may be of any given data type—numbers, strings, Booleans,

objects, arrays, or a combination of these—and there are rules that govern whether they

are equal. For example, two strings are equal when they have the same sequence of char-

acters, same length, and same characters in corresponding positions. Two numbers are

equal when they have the same numeric value. If a string is compared with a number,

they are equal if the number has the same characters as the string; for example,
“500”
is

equal to
500
.
NaN
(Not a Number) is not equal to anything, including
NaN
. Positive and

negative zeros are equal. Two objects are equal if they refer to the same object. Two Bool-

ean operands are equal if they are both
true
or both
false
.
Null
and
undefined
types are

equal. To test any of the expressions shown in Table 5.7, use the JavaScript Console. Fig-

ure 5.8 shows an example using Firefox.

Table 5.7
Equality Test with Strings and Numbers

Test

Are They Equal?

"William" == "William"

true

"william" == "William"

false

5 == 5.0

true

"54" == 54

true

"5.4" == 5.4

true

NaN == NaN

false

null == null

true

-0 == +0

true