The primitive data types of JavaScript
JavaScript provides for three primitive data types. The number data type is used to represent numerical data. The string data type is used to store character data. And the Boolean data type is used to store true and false values. This is summarized in figure 2-7.
The number data type can be used to represent either integers or decimal values. Integers are whole numbers, and decimal values are numbers that can have one or more decimal digits. The value of either data type can be coded with a preceding plus or minus sign. If the sign is omitted, the value is treated as a positive value. A decimal value can also include a decimal point and one or more digits to the right of the decimal point.
The value of a number data type can also include an exponent. If you're familiar with scientific notation, you know that this exponent indicates how many zeros should be included to the right or left of the decimal. Numbers that use this notation are called floating-point numbers. If you aren't familiar with this notation, you probably won't need to use it because you won't be coding applications that require it.
The number type can hold extremely large and small numbers so that is rarely an issue when you develop an application. For instance, the exponent of a number can indicate that the number should have 308 zeros to the left of the decimal point or 324 zeros to the right of the decimal point. However, if you try to store a number that is larger or smaller than the maximum and minimum values, which can happen if you divide a number by zero, the result will be stored as Infinity or -Infinity.
To represent string data, you code the string within single or double quotation marks (quotes). Note, however, that you must close the string with the same type of quotation mark that you used to start it. If you code two quotation marks in a row without even a space between them, the result is called an empty string, which can be used to represent a string with no data in it.
If you want to code a string that continues on a second line when it is displayed, you can use the \n escape sequence. As the example in this figure shows, this is equivalent to pressing the Enter key in the middle of a string. In chapter 7, you'll learn how to use some of the other escape sequences, but for now \n is the only one you need to know.
To represent Boolean data, you code either the word true or false with no quotation marks. As you will see, this data type can be used to represent data that is in one of two states such as yes/no, on/off, done/not done, or initialized/not initialized.
Examples of number values
15 // an integer-21 // a negative integer21.5 // a floating-point value-124.82 // a negative floating-point value-3.7e-9 // a floating-point value equivalent to -0.0000000037
The number data type
Examples of string values
"JavaScript" // a string with double quotes'String Data' // a string with single quotes"" // an empty string
How the \n escape sequence can be used to start a new line in a string
"A valid variable name\ncannot start with a number."// represents the string: A valid variable name// cannot start with a number.
The string data type
The two Boolean values
true // equivalent to true, yes, or onfalse // equivalent to false, no, or off
The Boolean data type
Description
Figure 2-7 The primitive data types of JavaScript