In this section, we'll visit the different Arithmetic Operators that you can use. These are commonly applicable for Numbers and BigInts, but there are some operators that you can even apply to Strings!
If you came from learning the Numbers data type, you're probably already familiar with how addition works. You can probably also guess what the rest of the possible arithmetic operators are aside from addition.
Shown below is a summary of the basic arithmetic operators we know from studying math (and a few more we might not have seen before).
- Log to the console the result of adding 3 and 5.
- Subtract 80 from 100 and then log the result to the console. Try it out the other way around, subtracting 100 from 80.
- Run
console.log(50 * 48);. - Run
console.log(100 / 5);. Also runconsole.log(5 / 100);. How aboutconsole.log(100 / 3);? - Using
**raises the number on the left to the power of the number on the right.console.log(5 ** 2);should result to 25. - Try out the following:
let x = 9;
x = x + 1;
console.log(x);
x += 3;
console.log(x);
This shows us one of the shortcuts for addition using the same variable. The same can also be done for -=, *=, and /=.
8. Incrementing and decrementing are even shorter shortcuts x += 1; or x -= 1; (specifically used only when incrementing by 1). Try it out with logging the result of x--; and x++;.
9. Finally, you can also declare the precedence of operators by using parentheses. (5 + 3) / 2 can be done to perform addition first before division.
