Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Chapter 5
Basic Declarations and Expressions

“Building” a Script

Writing a JavaScript script is similar in many ways to constructing a building; you need bricks as well as brick-laying instructions. Similarly, JavaScript, like most programming languages, provides variables (“bricks”) and instructions (“blueprints”) by which to build a script. Once you know how to use both elements, you can write useful programs for various purposes.

Functions are also an important ingredient of JavaScript. Just as all buildings are made of rooms, functions enable you to split a lengthy code into smaller sets of code. Basically, a script is a collection of functions.

Data Types in JavaScript

Every programming language can handle various types of information. For example, a number is a type of information that JavaScript recognizes. Such types of information are known as data types. Compared to most programming languages, JavaScript has a small number of data types, but its methods, objects, and other elements help overcome the problem. There are four different data types in JavaScript: numbers, strings, Boolean, and null values. As opposed to other languages, a variable data type is not declared explicitly but rather implicitly according to its initial value assignment. Also unique to JavaScript, there is no explicit distinction between integer and real-valued numbers.

All of these data types are specified in Table 5-1.

Table 5-1. Data types in JavaScript.


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

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

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.

Identifiers

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:

  • The first character of an identifier must be either a letter (uppercase or lowercase) or an underscore (_).
  • All other characters can be letters, underscores, or digits (0-9).
  • An identifier cannot be one of the language’s reserved words. Reserved words consist of all JavaScript keywords as well as others tokens reserved for future versions.

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

Naming Conventions

There are a number of generally accepted conventions in JavaScript:

  • A variable name is normally written in lowercase letters.
  • The variable name indicates its purpose and use in a program.
  • In a multiword identifier, either place an underscore between words or capitalize the first letter of each embedded word.

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


With any suggestions or questions please feel free to contact us