HTML and CSS Reference
In-Depth Information
Working with Data Types
So far, the examples you've explored have used variables that store text strings. However,
JavaScript variables can store different types of information. The type of information stored
in a variable is referred to as its data type . JavaScript supports the following data types:
• numeric value
• text string
• Boolean value
• null value
A numeric value is any number, such as 13, 22.5, or -3.14159. Numbers can also be
expressed in scientific notation, such as 5.1E2 for the value 5.1 × 10 2 (or 510). Numeric
values are specified without any quotation marks. Thus, if you wished to store the value
2007 in the year variable, you would use the statement
yearƒ=ƒ2007;
rather than
yearƒ=ƒ“2007”;
A text string is any group of characters, such as Hello or Happy Holidays! or 421
Sunrise Lane . Text strings must be enclosed within either double or single quotation
marks, but not a mix of both. The string value 'Hello' is acceptable, but the string value
“Hello' is not.
A boolean value indicates the truth or falsity of a statement. There are only two
Boolean values: true and false . For example, the following statement sets the value of the
useSafari variable to true and the value of the useIE variable to false :
useSafariƒ=ƒtrue;
useIEƒ=ƒfalse;
Boolean values are most often used in programs that must act differently based on
different conditions. The useSafari variable cited above might be used in a program that
tests whether a user is running the Safari browser. If the value is set to true , the program
might be written to run differently for the user than if the value were set to false .
Finally, a null value indicates that no value has yet been assigned to a variable. This
can be done explicitly using the keyword null in assigning a value to a variable, as in the
statement
If a Boolean variable's
value is left undefined, it is
interpreted by JavaScript
as having a value of false .
emLinkƒ=ƒnull;
or implicitly by simply declaring the variable without assigning it a value, as follows:
varƒemLink;
In either case, the emLink variable would have a null value until it was assigned a
value using one of the other data types.
In JavaScript, a variable's data type is always determined by the context in which it is
used. This means that a variable can switch from one data type to another within a single
program. In the following two statements, the Month variable starts out as a numeric
variable with an initial value of 5, but then becomes a text string variable containing the
text March:
Monthƒ=ƒ5;
Monthƒ=ƒ“March”;
A programming language like JavaScript, in which variables are not strictly tied to spe-
cific data types, is referred to as a weakly typed language . Some other programming lan-
guages, known as strongly typed languages , force the programmer to explicitly identify a
variable's data type. In those languages, the above code would result in an error because
a given variable would not be able to switch from one data type to another.
Search WWH ::




Custom Search