HTML and CSS Reference
In-Depth Information
When the bitwise | (OR) operator is applied to 1111 | 1001 , the result is binary
1111 or decimal 15.
When the bitwise ^ (Exclusive OR) is applied to 1111 ^ 1001 , the result is binary
0110 or decimal 6.
9<<2 yields 36, because 1001 shifted two bits to the left becomes 100100, which
is decimal 36.
9>>2 yields 2, because 1001 shifted two bits to the right becomes 10, which is dec-
imal 2.
-9 >> 2 yields -3 , because the sign is preserved.
15 >>> 2 yields 3, because 1111 shifted two bits to the right becomes 0011, which
is decimal 3. For nonnegative numbers, zero-fill right shift and sign-propagating
right shift yield the same result. (See Figure 5.19.)
Figure 5.19 Output from Example 5.13.
5.3 Number, String, or Boolean?
Data Type Conversion
As defined earlier, JavaScript is a loosely typed language, which really means that you don't
have to be concerned about what kind of data is stored in a variable. You can assign a num-
ber to x on one line and on the next line assign a string to x , you can compare numbers
and strings, strings and Booleans, and so on. JavaScript automatically converts values
when it assigns values to a variable or evaluates an expression. If data types are mixed (i.e.,
a number is compared with a string, a Boolean is compared with a number, a string is com-
pared with a Boolean), JavaScript must decide how to handle the expression. Most of the
time, letting JavaScript handle the data works fine, but there are times when you want to
force a conversion of one type to another. For example, if you prompt a user for input, the
input is set as a string. But, suppose you want to perform calculations on the incoming
data, making it necessary to convert the strings to numbers. When using the + operator
you want to add two numbers that have been entered as strings, not concatenate them, so
you will then need to convert the data from string to number.
Search WWH ::

Custom Search