Team LiB
Previous Section Next Section

Basic Data Types

Every variable has a data type that indicates what kind of data the variable holds. The basic data types in JavaScript are strings, numbers, and Booleans. A string is a list of characters, and a string literal is indicated by enclosing the characters in single or double quotes. Strings may contain a single character or multiple characters, including whitespace and special characters such as \n (the newline). Numbers are integers or floating-point numerical values, and numeric literals are specified in the natural way. Booleans take on one of two values: true or false. Boolean literals are indicated by using true or false directly in the source code. An example of all three data types follows.

var stringData = "JavaScript has strings\n It sure does";
var numericData = 3.14;
var booleanData = true;

JavaScript also supports two other basic types: undefined and null. All these data types as well as the details of special characters are discussed in Chapter 3. However, one aspect of JavaScript data types deserves special mention in this overview—weak typing.

Dynamic Typing

A major difference between JavaScript and many other languages readers might be familiar with is that JavaScript is dynamically typed (or, by some definitions, weakly typed). Every JavaScript variable has a data type, but the type is inferred from the variable’s content. For example, a variable that is assigned a string value assumes the string data type. A consequence of JavaScript’s automatic type inference is that a variable’s type can change during script execution. For example, a variable can hold a string at one point and then later be assigned a Boolean. Its type changes according to the data it holds. This explains why there is only one way to declare variables in JavaScript: there is no need to indicate type in variable declarations.

Being weakly typed is both a blessing and a curse for JavaScript. While weak typing appears to free the programmer from having to declare types ahead of time, it does so at the expense of introducing subtle typing errors. For example, given the following script that manipulates various string and number values, we will see type conversions cause potential ambiguities:

document.write("<<br />>");
document.write("5" + 5);
document.write("<<br />>");
document.write("5" - 3);
document.write("<<br />>");
document.write(5 * "5");

The output of this example when included in an HTML document is shown here:

Click To expand

Notice in most of the examples the string was converted to a number before calculation and the correct result was produced. Of course, if we would have attempted to do something like "cat" – 3, we would have seen a result of NaN because the string "cat" would convert to NaN and then the subtraction would produce NaN as well. However, in the case of the addition of "5" + 5, the answer was actually the string "55" rather than a number 10. The reason the addition didn’t work is that the plus sign serves two meanings, both as addition and as string concatenation.

Type conversion, coupled with overloaded operators like +, can create all sorts of confusion for the beginning and advanced programmer alike, so we spend a great deal of time on the subject in Chapter 3. Fortunately, the rules presented there are relatively logical and there are many ways to convert data predictably in JavaScript using methods like parseFloat() and to even check the value of a variable using the typeof operator. For example,

var x = "5";
alert (typeof x);

correctly identifies text after x as a string value, as shown here:

Team LiB
Previous Section Next Section