Calculations in JavaScript

Notes from April 8, 2005 class

Debugging:

In Mozilla/Firefox/Netscape, in the location bar, type in the following:

javascript:

and press "enter", to bring up the JavaScript console. This tool will alert you if there's a problem with any JavaScript code on a page you open in the Mozilla/Firefox/Netscape browser.

Increment/decrement

myVariable++
myVariable--

These are shortcuts for adding one to (or subtracting one from) myVariable while keeping its name as myVariable.

You can put the ++ or -- at the beginning or end of the variable name, but the location can change the outcome if you have additional operations going on.

this   will turn out to have an effect like this
myVar=myNumber++ -20   (myNumber-20) + 1
myVar=++myNumber -1   (myNumber + 1) -20

+=
-=
*=
are shortcuts for changing the variable by a set amount
myVar-=6 is the same as myVar=myVar-6

Operator Precedence

* and / (multiplication and division) happen before + and - (addition and subtraction); = happens last.

Between items with equal precedence, such as * and /, the order in which the calculations are performed happens in their left-to-right order.

In-class exercises:

Page 42, concatenating strings

Page 40, degrees calculator