|
Chapter 5
|
Type | Description | Examples |
Number | Any number without quotes | 42 or 16.3 or 2e-16 |
String | A series of characters enclosed in quote marks | "Hello!" or "10" or ' ' or "" |
Boolean | A logical value | true or false |
Null | A keyword meaning: no value | null |
The value null is often used to initialize variables that do not have any special meaning (see “Variable Declaration” later in the chapter). You assign it to a variable using the standard assignment statement:
var name = null
The null value is special in that it is automatically converted to initial values of other data types. When used as a number it becomes 0, when used as a string it becomes "", and when used as a Boolean value it becomes false. Since the source of many JavaScript errors is uninitialized variables, one of the common debugging techniques is to initialize all uninitialized variables, including the meaningless ones, with a null value.
JavaScript interpreter uses the null value on two occasions: (1) built-in functions return null under certain circumstances, and (2) non-existent properties evaluate to null. When checking for a null value, you should check if it is false or if it is equal to null.
Variables are the cornerstone of most programming and scripting languages. They serve as a link between simple words in your script and the computer allocated memory. There is a limit to the amount of memory you can use, but you should not reach that limit. JavaScript applications are not heavy resource demanders, so exceeding available memory is probably an indication of a bug in the browser or a major flaw in your program (such as an unexpected infinite loop).
Because you do not deal directly with the memory allocation, you should think of variables as baskets that contain items. You can put an item in a basket, take it out, or replace it with another. A script that does not use variables is probably useless. It’s like picking strawberries and taking them home in the palm of your hand, rather than using a basket—impossible! You need variables to keep data of different types.
Each variable is identified by a variable name, also known as an identifier. Each variable name is associated with a specific memory location, and the interpreter uses it to determine its location. There are strict rules for naming variables:
An identifier length is not limited, and you should take advantage of this feature to select meaningful names. JavaScript is case sensitive (uppercase letters are distinct from lowercase letters). For example, counter, Counter, and COUNTER are names of three different variables. Avoid using such similar identifiers in the same script.
Caution: The $ character is not legal in some versions of Netscape and in some other browsers, so it is recommended you avoid using it. Netscape did not designate the $ character as a valid identifier component, so future releases of the browser might not accept it. For example, the following identifiers are accepted by Netscape Navigator 3.0x and 4.0 but not by 2.0x and MSIE 3.0x.
$FirstDigit money$ tenth$cell$10
The following identifiers are legal:
gameCount _hamburger ____ _123456789_ look_at_me Number16
but the following ones are illegal:
with // reserved word ^fastTimer // first character is illegal 911phoneNumber // cannot start with a digit 04-825-6408 // first character is illegal // "-" is an illegal character ***important*** // * is not a legal character 10_guesses // first character cannot be a digit
There are a number of generally accepted conventions in JavaScript:
The following are examples of multiword identifiers.
all done // underscores allDone // capitalized letters
Avoid similar variable names. The following script illustrates a poor choice of variable names:
digit // current digit digits // number of digits in the number
A much better set of variables is:
current_digit // current digit num_of_digits // number of digits in the number
Previous | Table of Contents | Next |