|
Helping ordinary people create extraordinary websites! |
Essential Javascript -- A Javascript TutorialBy Patrick Hunlock2007-12-02
Arithmetic Operators There's nothing particularly exotic about the way Javascript does arithmetic.
++ and -- are C style operators their position around the variable they are operating on is important. If the operator appears BEFORE the variable they will be evaluated immediately, if they appear AFTER the variable they will be evaluated only after the entire line has been resolved. For instance… var x = 5; In the first example y is assigned the value of x (5) and then x is incremented by one because the ++ operator appears AFTER x. In the second example, x is incremented by one first because ++ appears BEFORE the x, so y is given the value 6. How this works between C and PhP and Javascript can be somewhat different. There was a most excellent discussion about this on reddit if you'd like to delve into those differences. Additional shorthand operators exist when working on the same variable. For instance… x = x + 5; // is the same as... Tutorial Pages: » Essential Javascript -- A Javascript Tutorial » Getting Started » In-Line Javascript » External Javascript » Javascript is case sensitive » Output (writeln) » Output (alert) » Output (getElementById) » Input (One Click To Rule Them All) » Input (User Input) » Javascript is an Event Driven Language » Comments » Variables » Variable Scope » Special Keywords » Arithmetic Operators » Logical and Comparison Operators » JavaScript Conditionals: IF » JavaScript Conditionals: SWITCH » JavaScript Conditionals: Shorthand Assignment » JavaScript Conditionals: Ternary Operators » JavaScript Loops: FOR » JavaScript Loops: FOR/IN » JavaScript Loops: WHILE » Bringing It All Together » DHTML: Dynamic HTML » Conclusion copyright © 2006, 2007 by Patrick Hunlock |
|