FREE BOOK

Chapter 2: How to code a JavaScript application

Posted by Murach Free Book | Internet & Web November 02, 2009
This chapter presents a subset of JavaScript and DOM scripting that will soon have you writing significant applications. If you don't have any programming experience, this chapter also makes a great aptitude test. If you read it and can do the exercises at the end of the chapter, you're ready for the rest of this book.

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 integer
21.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

  • The number data type is used to represent an integer or a decimal value.
  • An integer is a whole number that can start with a positive or negative sign.
  • A floating-point value consists of a positive or negative sign at the beginning, digits, an optional decimal point, optional decimal digits, and an optional exponent.
  • An exponent consists of an upper or lowercase e, followed by a positive or negative sign and a whole number. The whole number must be between -324 and +308.
  • If a result is stored in the number data type that is larger or smaller than the data type can store, it will be stored as the value Infinity or –Infinity.

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 string data type represents character (string) data. A string is surrounded by double quotes or single quotes. The string must start and end with the same type of quote mark.
  • An empty string is a string that contains no characters. It is entered by typing two quotation marks with nothing between them.
  • JavaScript provides for several escape sequences that can be used within strings. All escape sequences start with the backslash, and the \n escape sequence starts a new line.

The two Boolean values

true // equivalent to true, yes, or on
false // equivalent to false, no, or off

The Boolean data type

  • The Boolean data type is used to represent a Boolean value. A Boolean value can be used to represent data that is in either of two states.

Description

  • JavaScript provides for just three primitive data types. These data types are used for storing numbers, strings, and Boolean values.

Figure 2-7 The primitive data types of JavaScript

Total Pages : 20 678910

comments