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

To see the effect of a carriage return character, try displaying the last string in an alert box.

Making Statements

A statement is a line of JavaScript code that includes a keyword. This is the official definition of a statement by Netscape’s JavaScript documentation. Most people tend to associate this term with any line of code, with or without a keyword. In this book every line of JavaScript code is a statement. For example, we call the following line a statement, although it does not contain any keywords:

yourAge = 16

Keywords are words that have special meanings in the language. You cannot use keywords to name elements you created in a script (such as variables, functions, etc.). The list of keywords is the basic vocabulary of the language. The word if, for example, is a keyword. You do not have to memorize the list, because you will gradually remember it as you use the words in your scripts. Some keywords do not have any role in the current version of the language, but they will probably be added to a future version. Were they not defined as keywords, people would use them as identifiers, making their scripts incompatible with future versions.

Multiple Statements

The JavaScript interpreter accepts multiple statements on the same line. If you choose to use this method, you must separate the statements with semicolons (;). The last statement of the line does not have to be followed by a semicolon. Such a line looks like this:

statement1; statement2; statement3; ...

The browser interprets these statements as if they were listed on separate lines:

statement1
statement2
statement3
.
.
.

Normally, you should not place multiple statements on the same physical line. Multiple statements on one line are difficult to find and make the debugging process difficult.

If it is one of your programming habits, you may terminate all statements with semicolons,

statement1;
statement2;
statement3;
.
.
.

Nested Statements

In the previous chapter we talked about command blocks. A command block is a unit of statements, enclosed by curly braces. It is very important to understand that a block should be used as a single statement. The statements inside the block are called nested statements:

{
nested statement1
nested statement2
nested statement3
.
.
.
}

A loop that includes many statements is actually one statement with many nested statements. This rule applies to functions, if - else statements, and other language elements.

Simple Assignment Statements

You’ve already learned how to assign a value to a variable or to initialize it, using the equal assignment operator. As the following piece of code demonstrates, you can also perform calculations when you assign a value:

/* 1 */ var answer
/* 2 */ answer = 4 * 2 + 9
/* 3 */ document.write(answer)

Line 1 includes the declaration of the variable answer. The second line shows how the variable answer is assigned the result of a simple mathematical expression. The operators used in this statement are discussed in Chapter 7, Utilizing JavaScript Operators. At this point, the variable holds a value of 17. Referring to the variable answer is the same as referring to the number 17. For this reason, the statement on line 3 prints the number 17.


Caution:  A very common mistake is to use the equal sign for equality check. In Pascal, for example, “=” is an equality test operator, because the basic assignment operator of the language is “:=”. However, in JavaScript, like in C++ and Java, “=” (the equal sign operator) is an assignment operator, while “==” (two equal signs) is an equality test operator. See Chapter 7, Utilizing JavaScript Operators, for details on both operators.


Evaluating Expressions

Now that you know how to create a variable, you need to know how to use it. As mentioned earlier, variables hold values of different types. What does “holding a value” mean?

This term refers to expression evaluation. A variable always evaluates to its value. When you perform an operation on a variable, you are actually performing the operation on the current value associated with the variable. Let’s assume you created a variable named firstNumber using the following statement:

var firstNumber = 120 // declaration and initialization


Figure 5-3.  The “javascript:” expression evaluation tool.

At this point, if you refer to the variable firstNumber, its value, 120, is returned. That is, firstNumber is evaluated to 120. The following statement outlines an evaluation of firstNumber:


Figure 5-4.  The “mocha” expression evaluation tool (identical to “javascript:”).

secondNumber = firstNumber * 6

The secondNumber variable now holds the value 720, because firstNumber evaluates to 120. Bear in mind that no link between the memory locations of both variables is established. Therefore, secondNumber now holds a value of 720, which does not change even if the value of firstNumber changes. A variable can evaluate to a value of any type (see Table 5-1).

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us