Operators and expressions
Arithmetic operators
Javascript provides several arithmetic operators that let you perform mathematical calculations.
Symbol | Name | Description |
---|---|---|
+ | Addition | Adds two values |
- | Subtraction | Finds the difference between two values |
* | Multiplication | Multiplies two values |
/ | Division | Divides one value by another |
** | Exponentiation | Raises one value to the power of another |
% | Remainder | Calculates the remainder on division |
Expressions
An expression in javascript is a collection of operators and variables that evaluates down to a single value.
js
const bookPrice = 15 // variable
const numberOfBooks = 100 // variable
const totalValue = bookPrice * numberOfBooks // expression
console.log(totalValue)
console
1500
Modifying a variable
When updating a variable, we can use its old value to calculate its new value.
js
let availableBooks = 120
availableBooks = availableBooks + 10
console.log(availableBooks)
console
130
We can do this a bit more quickly with +=
and -=
.
js
let availableBooks = 120
availableBooks += 10
console.log(availableBooks)
availableBooks -= 5
console.log(availableBooks)
console
130
125