HTML and CSS Reference
In-Depth Information
put in (or assign to) the variable. For example, if you're making a Web site that expects to be
used to enter prices and tax (as was done in the previous section, “HTML5 elements and the
DOM”), it makes sense to use meaningful variable names such as “cost” and “tax.”
To create a variable, you simply provide a name and assign it a value. For example,
billVar = ”Brought to you by Bill's variable.” ;
alert ( billVar );
creates a variable named billVar . It then assigns it the value Brought to you by
Bill's variable . When the variable is placed in the alert function, notice that no
quotation marks surround the variable.
Types of data
When you assign values to a JavaScript variable, you can assign any kind you want and then
change it to a dif erent type. First, though, you need to have an idea of the dif erent types of
data that are available. h e following list provides a brief description of each:
String: Treated as text, typically in quotation marks
Number: An actual number (integer or real) that responds to math operations
Boolean: A two-state (true or false, 0 or 1) data type
Function: A set of JavaScript operations contained in a module
Object: An encapsulated collection of properties (variables/arrays) and methods
You've seen how string variables work. When you put numbers into a string, they're treated as
text instead of numbers. For example, the following string treats the “123” exactly like “Elm
Street” — as text.
funHouse = ”123 Elm Street” ;
Likewise, if you used the following assignment, you'd still have text and the results would
show it:
firstNumber = ”123” ;
secondNumber = ”7” ;
total = firstNumber + secondNumber ;
document . write ( total );
Instead of showing “130” the results show “1237.” Next try the following:
firstNumber = 123 ;
secondNumber = 7 ;
total = firstNumber + secondNumber ;
document . write ( total );
Search WWH ::

Custom Search