↑

Main Page

# Boolean operators

First, look at the true 32-bit representation of –64. To do so, you need to create an unsigned version of the

number, which can be attained by using unsigned right shift with a bit count of 0:

var iUnsigned64 = 64 >>> 0;

Then, to get the actual bit representation, use the

toString()

method of the Number type with a

radix of 2:

alert(iUnsigned64.toString(2));

This yields a value of 11111111111111111111111111000000, which is the two’s complement representation of

–64 for a signed integer, but it is equal to 4294967232 as an unsigned integer. For this reason, use caution

with the unsigned right shift operator.

Boolean operators

Almost as important as equality operators, Boolean operators are what make a programming language

function. Without the capability to test relationships between two values, statements such as

if...else

and loops wouldn’t be useful. There are three Boolean operators: NOT, AND, and OR.

Logical NOT

The logical NOT operator in ECMAScript is the same as in C and Java, indicated by an exclamation

point (

!

). Unlike logical OR and logical AND operators, the logical NOT always returns a Boolean value.

The logical NOT operator behaves in the following way:

?

If the operand is an object,

false

is returned.

?

If the operand is the number 0,

true

is returned.

?

If the operand is any number other than 0,

false

is returned.

?

If the operand is

null

,

true

is returned.

?

If the operand is

NaN

,

true

is returned.

?

If the operand is

undefined

, an error occurs.

This operator is typically used in control loops (discussed later):

var bFound = false;

var i = 0;

while (!bFound) {

if (aValues[i] == vSearchValue) {

bFound = true;

} else {

i++;

}

}

43

ECMAScript Basics

05_579088 ch02.qxd 3/28/05 11:35 AM Page 43

Free JavaScript Editor
Ajax Editor

©

↓