Java Reference
In-Depth Information
So we will just look at this line here. You can see that the alert() function contains an expression.
Let's look at that expression more closely.
First is the variable degFahren , which contains numerical data. You concatenate that to the string
“\xBO Fahrenheit is . JavaScript realizes that because you are adding a number and a string, you
want to join them together into one string rather than trying to take their sum, and so automatically
converts the number contained in degFahren to a string. You next concatenate this string to the vari-
able degCent , containing numerical data. Again JavaScript converts the value of this variable to a
string. Finally, you concatenate to the string “\xBO centigrade” .
Note also the escape sequence used to insert the degree character into the strings. You'll remember
from earlier in the chapter that \xNN can be used to insert special characters not available to type in
directly. ( NN is a hexadecimal number representing a character from the Latin-1 character table). So
when JavaScript spots \xB0 in a string, instead of showing those characters it does a lookup to see what
character is represented by B0 and shows that instead.
Something to be aware of when using special characters is that they are not necessarily cross-platform-
compatible. Although you can use \xNN for a certain character on a Windows computer, you may fi nd
you need to use a different character on a Mac or a Unix machine.
You'll look at more string manipulation techniques in Chapter 4 — you'll see how to search strings
and insert characters in the middle of them, and in Chapter 8 you'll see some very sophisticated string
techniques.
Data Type Conversion
As you've seen, if you add a string and a number, JavaScript makes the sensible choice and converts the
number to a string, then concatenates the two. Usually, JavaScript has enough sense to make data type
conversions like this whenever it needs to, but there are some situations in which you need to convert
the type of a piece of data yourself. For example, you may be given a piece of string data that you want
to think of as a number. This is especially likely if you are using forms to collect data from the user. Any
values input by the user are treated as strings, even though they may contain numerical data, such as the
user's age.
Why is changing the type of the data so important? Consider a situation in which you collect two num-
bers from the user using a form and want to calculate their sum. The two numbers are available to you
as strings, for example “22” and “15”. When you try to calculate the sum of these values using “22” +
“15” you get the result “2215”, because JavaScript thinks you are trying to concatenate two strings
rather than trying to fi nd the sum of two numbers. To add to the possible confusion, the order also
makes a difference. So:
1 + 2 + 'abc'
results in a string containing “3abc”, whereas:
'abc' + 1 + 2
would result in the string containing “abc12”.
Search WWH ::




Custom Search