Exponents, Precedence and Augmented Assignment
Raising to a power
Two stars in a row are the exponent operator. The number on the left is the base, the number on the right is the power.Exponentiation is the one operator that groups right to left. Every other operator on the same precedence row groups left to right.
Unary minus
A minus sign written in front of a single value is negation. It is a unary operator: it works on one operand. The minus in7 - 2 is a binary operator, working on two.Operator precedence
| Precedence | Operator | Operation |
|---|---|---|
| highest | ( ) | grouping |
++ -- and unary - | increment, decrement, negation | |
** | exponentiation (applied right to left) | |
* / % | multiplication, division, remainder (left to right) | |
| lowest | + - | addition, subtraction (left to right) |
The program below works out four expressions, with and without parentheses. Run it, then change it and run it again:
• In the line for
a, put parentheses around 3 * 2.• In the lines for
c and d, change the power from 2 to 3.• In the line for
c, delete the parentheses.let a = 7 + 3 * 2 ** 2;
let b = (7 + 3) * 2 ** 2;
let c = (-6) ** 2;
let d = -(6 ** 2);
console.log("a is", a);
console.log("b is", b);
console.log("c is", c);
console.log("d is", d);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Augmented assignment
An augmented assignment folds an operator into an assignment so the line stays short. The whole right-hand side is worked out first, then the operator is applied to the variable.| Shorthand | Longer version |
|---|---|
x += n | x = x + n |
x -= n | x = x - n |
x *= n | x = x * n |
x /= n | x = x / n |
x %= n | x = x % n |
x **= n | x = x ** n |
Increment and decrement
Adding or subtracting one is common enough that JavaScript gives it its own operators.++ adds one to a variable, -- subtracts one. Each comes in two forms, and the difference only shows when the line also uses the value the operator hands back.| Form | Effect on n | What the operator hands back |
|---|---|---|
n++ | n goes up by one | the value n held BEFORE |
++n | n goes up by one | the value n holds AFTER |
n-- | n goes down by one | the value n held BEFORE |
--n | n goes down by one | the value n holds AFTER |
The program below changes a count of marbles with augmented assignment and then with
++, and prints the count after each change. Run it, then change it and run it again:• Change
marbles *= 2 + 1 to marbles = marbles * 2 + 1.• Change
marbles++ to ++marbles.• Change
marbles++ to marbles--.let marbles = 15;
marbles += 8;
console.log("marbles:", marbles);
marbles *= 2 + 1;
console.log("marbles:", marbles);
console.log("handed back:", marbles++);
console.log("marbles:", marbles);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.