8.3 Data Types, Constants, Variables
The kinds of values that can be expressed in a programming language are known as its data types. The Watson Graphics Language of Chapter 6 supported data types relevant to drawing graphical images, such as distance, point, circle, and color. Watson JavaScript supports only two data types: text and numbers[1]. The Text data type gives Watson JavaScript the ability to represent non-numeric data such as names, addresses, English phrases, etc. The Numeric data type allows the language to manipulate numbers, both positive and negative, whole numbers and fractions.
You may recall from your study of the Watson Graphics Language that a constant was defined as a value of a particular type that does not change over time. Both numbers and text may be expressed as constants in Watson JavaScript. Numeric constants are composed of the digits 0 through 9 together with an optional leading “+” or “-” sign and an optional decimal point. Numeric constants may not contain commas, dollar signs, or any other special symbols. The following are valid Watson JavaScript numeric constants:
+15 -150 15.01 3200
Due to the nature of the underlying physical hardware, almost all programming languages impose limits on the range of numeric values they can represent. Full JavaScript stores numbers in a standard form called IEEE 754 Double Precision, which supports a very wide range, approximately +/- 10 +308 which is either a “1” or “-1” followed by 308 zeros. Even though the overall range is quite large, the number of significant digits is generally limited to about 15 or so. During our discussion of Watson JavaScript we can safely ignore numeric range limits, since all of the problems we will be working on will use numbers well within the supported range.
A text constant consists of a sequence of characters enclosed in double quote marks. The following are examples of valid Watson JavaScript string constants:
The value of a text constant is the actual sequence of characters appearing between the quote marks. Hence, The Holy Grail is the value of the text constant “The Holy Grail”. The only character disallowed in a text constant is the double quote mark itself, since that symbol is used to mark the beginning and end of a text string. Hence, the following is not a valid text constant:
In Chapter 6, a variable was defined to be a named object that can store a value of a particular type. Watson JavaScript supports two types of variables: text string variables and numeric variables. Before a variable can be used, its name and type must be declared. All variable declarations in Watson JavaScript are of the form:
var name; //type
Hence, a numeric variable named age could be declared with the following statement:
var age; // Numeric
Similarly, a text variable named firstName could be declared with the statement:
var firstName; // Text
It is important for you to realize that while human programmers generally try to give variables names that reflect the use to which they will be put, the variable name itself doesn’t mean anything to the computer. For example, the numeric variable age can be used to hold any number[2], not just an age. It is perfectly legal for age to hold the number of students in a class, or the number of eggs in your refrigerator. The computer couldn’t care less. Human programmers, on the other hand, generally care a great deal. They expect a variable’s name to accurately reflect its purpose, so while it is possible to do so, it would be considered poor programming practice to use the variable age to store anything other than an age.
Exercises for Section 8.3
Identify each of the following items as: (1) a numeric constant, (2) a text constant, or (3) neither a valid numeric nor text constant. If the item is neither a valid numeric nor text constant, explain why.
a. |
1999 |
|
f. |
“green eggs and ham” |
|
k. |
3.14159 |
b. |
21,256 |
|
g. |
$5.00 |
|
l. |
+15 |
c. |
“Sam” |
|
h. |
Train |
|
m. |
“$5.00” |
d. |
25 |
|
i. |
2,001 |
|
n. |
37 |
e. |
99999999 |
|
j. |
“15” |
|
o. |
“the letter “A” ” |
Write a Watson JavaScript statement that declares a variable named bestFriend capable of holding a text value.
Write a Watson JavaScript statement that declares a variable named age capable of holding a numeric value.
Footnotes
[1] Full JavaScript includes a third data type called Boolean. Additionally, the type we refer to as “text” in this chapter is generally called “string” when discussing full JavaScript, as opposed to Watson JavaScript.
[2] Within the supported range.