HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [138]
JavaScript is much more easygoing about variable types. When you make a variable, you can put any kind of data in it that you want. In fact, the data type can change. A variable can contain an integer at one point, and the same variable may contain text in another part of the program.
JavaScript uses the context to determine how to interpret the data in a particular variable. When you assign a value to a variable, JavaScript puts the data in one of the following categories:
♦ Integers are whole numbers (no decimal part). They can be positive or negative values.
♦ A floating point number has a decimal point — for example, 3.14. You can also express floating point values in scientific notation, such as 6.02e23 (Avogadro’s number –6.02 times 10 to the 23rd). Floating point numbers can also be negative.
♦ A Boolean value can only be true or false.
♦ Text is usually referred to as string data in programming languages. String values are usually enclosed in quotes.
♦ Arrays and objects are more complex data types that you can ignore for now.
Most of the time, when you make a variable, JavaScript guesses right, and you have no problems. But sometimes, JavaScript makes some faulty assumptions, and things go wrong.
The pesky plus sign
I use the plus sign in two ways throughout this chapter. The following code uses the plus sign in one way (concatenating two string values):
var x = “Hi, “;
var y = “there!”;
result = x + y;
alert(result);
In this code, x and y are text variables. The result = x + y line is interpreted as “concatenate x and y,” and the result is “Hi, there!”
Here’s the strange thing: The following code is almost identical.
var x = 3;
var y = 5;
result = x + y;
alert(result);
Strangely, the behavior of the plus sign is different here, even though the statement result = x + y is identical in the two code snippets.
In this second case, x and y are numbers. The plus operator has two entirely different jobs. If it’s surrounded by numbers, it adds. If it’s surrounded by text, it concatenates.
That’s what happened to the first adding machine program. When the user enters data in prompt dialogs, JavaScript assumes that the data is text. When I try to add x and y, it “helpfully” concatenates instead.
There’s a fancy computer science word for this phenomenon (an operator doing different things in different circumstances). Those Who Care about Such Things call this mechanism an overloaded operator. Smart people sometimes have bitter arguments about whether overloaded operators are a good idea because they can cause problems like this one, but they can also make things easier in other contexts. I’m not going to enter into that debate here. It’s not really a big deal, as long as you can see the problem and fix it.
Changing Variables to the Desired Type
If JavaScript is having a hard time figuring out what type of data is in a variable, you can give it a friendly push in the right direction with some handy conversion functions, as shown in Table 1-1.
Table 1-1 Variable Conversion Functions
Function
From
To
Example
Result
parseInt()
String
Integer
parseInt(“23”)
23
parseFloat()
String
Floating point
parseFloat(“21.5”)
21.5
toString()
Any variable
String
myVar.toString()
varies
eval()
Expression
Result
eval(“5 + 3”)
8
Math.ceil()
Math.floor()
Math.round()
Floating point
Integer
Math.ceil(5.2)
Math.floor(5.2)
Math.round(5.2)
6
5
5
Using variable conversion tools
The conversion functions are incredibly powerful, but you only need them if the automatic conversion causes you problems. Here’s how they work:
♦ parseInt() is used to convert text to an integer. If you put a text value inside the parentheses, the function returns