18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ECMAScript Basics<br />

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

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

var iUnsigned64 = 64 >>> 0;<br />

Then, to get the actual bit representation, use the toString() method of the Number type with a<br />

radix of 2:<br />

alert(iUnsigned64.toString(2));<br />

This yields a value of 11111111111111111111111111000000, which is the two’s complement representation of<br />

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

with the unsigned right shift operator.<br />

Boolean operators<br />

Almost as important as equality operators, Boolean operators are what make a programming language<br />

function. Without the capability to test relationships between two values, statements such as if...else<br />

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

Logical NOT<br />

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

point (!). Unlike logical OR and logical AND operators, the logical NOT always returns a Boolean value.<br />

The logical NOT operator behaves in the following way:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

If the operand is an object, false is returned.<br />

If the operand is the number 0, true is returned.<br />

If the operand is any number other than 0, false is returned.<br />

If the operand is null, true is returned.<br />

If the operand is NaN, true is returned.<br />

If the operand is undefined, an error occurs.<br />

This operator is typically used in control loops (discussed later):<br />

var bFound = false;<br />

var i = 0;<br />

while (!bFound) {<br />

if (aValues[i] == vSearchValue) {<br />

bFound = true;<br />

} else {<br />

i++;<br />

}<br />

}<br />

43

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!