Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Javascript for dummies 4th edition - Veer E.V

Veer E.V Javascript for dummies 4th edition - Wiley publishing , 2004. - 387 p.
ISBN: 0-7645-7659-3
Download (direct link): javascriptfordummies2005.pdf
Previous << 1 .. 17 18 19 20 21 22 < 23 > 24 25 26 27 28 29 .. 118 >> Next

50 Part I: Building Killer Web Pages for Fun and Profit__________________________________________________________
You can test this scrap of code for yourself. It should produce the following result:
1 2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20
Operators are standing by
Operators are like conjunctions. Remember fifth-grade English? (Or if you were a cartoon connoisseur, maybe you remember “Conjunction Junction.” “And, but, and or, get you pretty far. . . .”) Ahem.
Operators, like conjunctions, enable you to join multiple phrases together to form expressions. If you’re familiar with addition and subtraction, you’re familiar with operators. Two categories of operators exist:
^ Binary: Two items (or operands) must be sandwiched on either side of the operator.
^ Unary: Only one operand is required.
Table 3-1 gives you a rundown of the basic operators. The JavaScript interpreter always evaluates the expression to the right of the equal sign first, and only then does it assign the evaluated value to the variable. (Note: The two exceptions to this rule include the unary decrement operator (--), and the unary increment operator (++). In these cases, if you place the operand after the operator — as in the expression --1 — the JavaScript interpreter evaluates the expression before evaluating anything else in the statement, including any assignment. Check out Table 3-1 for more information about the decrement and increment operators.)
Table 3-1 JavaScript Operators
In all these examples, x is initially set to 11.
Operator Meaning Example Result How Come?
о о modulus x = x % 5 x = І 11 / 5 = 2 with 1 remainder, so modulus returns 1 in this case
++ increment x = x++ x = ІІ ++ is applied after assignment when you put it after x
x = ++x x = І2 ++ is applied before assignment when you put it before x
-- decrement x = x-- x = ІІ -- is applied after assignment when you put it after the var
x = --x x = І0 -- is applied before assignment when you put it before the var
Chapter 3: JavaScript Language Basics
In all these examples, x is initially set to 11.
Operator Meaning Example Result How Come?
- negation x = -x x = -11 Turns positive numbers negative and vice versa
+ addition x = x + x x = 22 11 + 11 is 22
Some of the operators are pretty normal (addition and negation, for example). The increment and decrement operators are a little weird, though, because not only are they a new thing (you never see ++ or -- outside a computer program listing), but depending on whether you put them before or after the variable, they behave differently, as I describe in Table 3-1.
Operator precedence
Just as in math, an order of evaluation is applied to a statement that contains multiple operators. Unless you set phrases off with parentheses, the JavaScript interpreter observes the precedence order shown in Table 3-2 (from the semicolon, which has the lowest order of precedence, to the parentheses, which has the highest).
Table 3-2 JavaScript Operator Precedence (From Lowest to Highest)
Operator Syntax
semicolon ; (separates JavaScript statements that appear on the same line)
comma ,
assignment =, +=, -=, *=, /=, %=
conditional ?:
logical "or" jj
logical "and" &&
equality ==, ! =
relational <, <=, >, <=
mathematical +, -, *, /, %
unary !, -, ++, -- (negation, increment, and decrement operators)
call ()
Part I: Building Killer Web Pages for Fun and Profit
So, how exactly does operator precedence work? Well, suppose the JavaScript interpreter runs into the following statement in your script:
alert("Grand total: ” + getTotal() + (3 * 4 / 10) + tax++)
The interpreter knows that its job is to evaluate the statement, so the first thing that it does is scan everything between the alert() parentheses. When it finds the next set of parentheses, it knows that’s where it needs to start. It thinks to itself, “Okay, first I’ll get the return value from getTotal(). Then I’ll evaluate (3 * 4 / 10). Within (3 * 4 / 10), I’ll do the division first, and then the multiplication. Now I’ll add one to the tax variable. Okay, the last thing I have to do is add the whole thing to come up with a string to display.”
Frankly, it’s okay if you can’t remember the precedence order. Just group expressions in parentheses like you did back in high school algebra class. Because parentheses outrank all the other operators, you can force JavaScript to override its default precedence order and evaluate expressions the way that makes the most sense to you!
Assignment operators
Assignment operators enable you to assign values to variables. Besides being able to make a straight one-to-one assignment, though, you can also use some assignment operators as a kind of shorthand to bump up a value based on another value. Table 3-3 describes how this process works.
Table 3-3 JavaScript Assignment Operators (From Lowest to Highest Precedence)
Assignment Alternate Approach Description
Previous << 1 .. 17 18 19 20 21 22 < 23 > 24 25 26 27 28 29 .. 118 >> Next