Table of Contents
- Supported Arithmetic Operators
- Examples of Arithmetic Operators
The best way to show these supported operators is by using a table. The table below shows you all the information you need.
For example, for any chosen operator, you can see its name, an example of using that operator, and a short description of the result.
|+||Addition||x + y||Adds |
|–||Subtraction||x – y||Subtracts |
|*||Multiplication||x * y||Multiplies |
|**||Exponentiation||x ** y||Raises |
|/||Division||x / y||Divides |
|%||Modulus (Division Remainder)||x % y||Returns remainder of |
|x++||Post-Increment||x++||Returns the value, then increments |
|x–||Post-Decrement||x++||Returns the value, then subtracts |
To learn more about each of these arithmetic operators, then be sure to read on. We will give a quick example of each of these operators and explain more about what they do.
Within each of these examples, we will explain further what the operator is used for and what math operation they perform.
+) is straightforward to use and allows you to add two values together easily.
In our example below we simply add the number
10 and log the result to the console. All we needed to do was use the plus (
+) symbol in between our two values.
console.log(10 + 10);
After running the example below, you should end up with the following value.
Subtraction Operator (
The subtraction operator (
With this example, we are subtracting
15 (right value) from
20 (left value). The result from this operation is logged to the console.
console.log(20 - 15);
15 being subtracted from
The multiplication operator (
To showcase this, we have the following example where we use the asterisk symbol (
*) to multiply
2. The value returned by this operation is logged to the console.
console.log(5 * 2);
Below you can see how our number
5 was multiplied by the value
2, giving us the value of
Exponentiation Operator (
The exponentiation operator (
To showcase this, in our example below, we will be raising
10 to the power of
2. This would be the equivalent of writing “
10 * 10“. To showcase this operation’s end value, we log it to the console.
console.log(10 ** 2);
10 to the power of
With this example, we will divide
2 and log the resulting value to the console.
console.log(100 / 2);
The result below shows how our use of the division operator divided our value of 100 by 2.
Modulus Operator (Division Remainder) (
The modulus operator is somewhat similar to the division operator, but instead of returning the divided value, it only returns what is left over by the operation.
For example, dividing
10 will give us the remainder of “
%) to get the division remainder of
50 divided by
console.log(50 % 35);
Below you can see the result of this modulus operation. Since
35 only goes into
50 once, we will end up with a remainder of
The pre-increment operator (
++) directly before your variable.
To showcase this, we start by declaring a variable called “
x” and assigning it the numerical value of
We then log the value of “
x” by using the “
let x = 15; console.log(++x);
Since the value of “
x” is incremented before it is returned, you should end up with the following result.
Post-Increment Operator (
The post-increment operator (
++) immediately after your variable.
With the example below, we start the script by declaring the “
x” and assigning it the number
We then post-increment the value of “
x” and print the resulting value returned by this operation. We also log the value of our “x” variable immediately after.
let x = 16; console.log(x++); console.log(x);
Below you can see how the post-increment operator works. The first log prints the original value since the post-increment operator returns the value immediately.
The pre-decrement operator (
--) immediately before your variable name.
With the example below, we start our script by declaring a variable called “
x” and assigning it the value of
x” variable and log the resulting value.
let x = 17; console.log(--x);
x” variable by
Post-Decrement Operator (
x--). This operator returns the value of the variable, and then decrements the value of variable by one.
You can utilize this operator by using two minus symbols (
--) immediately after your variable name.
x” and assign it the value
For our first “
console.log()” call, we use the post-decrement operator to decrease the value of our “
x” variable. Since this is a “post” decrement, the returned value will be the original one, not the decreased value.
We use a second “
console.log()” to show you how the value is only updated after it has been returned.
let x = 16; console.log(x--); console.log(x);
These operators allow you to perform math on your values within your scripts easily.