|
ExpressionsThe term expression has been mentioned dozens of times throughout this chapter. An expression is any valid set of literals, variables, operators, and other expressions that evaluates to a single value. The value may be a number, a string, or a Boolean value. Conceptually, there are two types of expressions:
The following are expressions:
a = "Shiran" "Netscape" false b = true
The first type of expression is a bit more difficult to understand. In such expressions, an assignment is performed. The entire specification, including both operands of the assignment operator and the operator itself, is evaluated to the assigned value (“Shiran”). Consider the following statement:
document.write(x = "Israel")
This statement prints Israel, because the whole expression evaluates to the assigned value. The next section discusses side effects which take advantage of such expressions. Side EffectsA side effect is an operation performed in addition to the main one. Take a look at the following statements:
number = 3 answer = ++number
The first line is a simple assignment statement. The second line is a bit more complicated. It performs two actions in the following order:
Remembering the order in which the actions take place is not necessary. You could break down the side effect and use two different statements instead:
number = 3 number++ answer = number
This set of statements is easy to follow. The only “advantage” of the first method is that the code is compact. Compact code is only a holdover from the early days of computing, when storage was expensive, and programmers used various compacting techniques to save disk space. The situation is different today, because the number one rule in programming is to keep programs clear and simple. It comes even before efficiency, especially in simple programs like the ones written in JavaScript (you will not find any 500,000-line scripts in this advanced book). Other side effects are not related to the increment and decrement operators. Consider the following JavaScript statement:
document.write(myName = "Yehuda")
You already know that this statement prints Yehuda. The assignment operation also takes place in this statement, so myName holds the value Yehuda after the statement. The assignment operation is the side effect, and the printing action is the main action. The preceding statement should be split into two separate statements:
myName = "Yehuda" document.write(myName)
Here are some more “Morse” statements you should avoid (they will keep you busy for a while...):
number = 0 answer = (number++ – 2) * (--number – 1) document.write(number = answer++)
This set of statements prints 2. Let’s analyze it:
The final result:
The following script is longer, but it is definitely better:
number = 0 answer = number – 2 number++ number-- answer *= number – 1 number = answer document.write(number) number = amswer answer++
SummaryIn this chapter we discussed JavaScript’s wealth of operators. Some of them are extremely useful, while others are not used at all. We also discussed various expression forms. In addition, we played around with some side effects. Never use side effects in your programs, because they are hard to follow and difficult to understand. The next chapter will discuss control structures and will show how useful the wide variety of operators is.
|
|||||||||||||||||||||||
With any suggestions or questions please feel free to contact us |