Skip to main content

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numbers. JavaScript provides several arithmetic operators that allow you to add, subtract, multiply, and divide numbers. You can also use the modulus operator to find the remainder of a division operation and the exponentiation operator to raise a number to a power.

Here are the arithmetic operators in JavaScript:

  1. Addition (+): Adds two numbers.
  2. Subtraction (-): Subtracts one number from another.
  3. Multiplication (*): Multiplies two numbers.
  4. Division (/): Divides one number by another.
  5. Modulus (%): Returns the remainder of a division operation.
  6. Exponentiation (**): Raises a number to a power.
  7. Increment (++): Increases the value of a variable by 1.
  8. Decrement (--): Decreases the value of a variable by 1.

Let's explore each arithmetic operator in more detail with examples.

Addition Operator (+)

Here is an example of using the addition operator to add two numbers:

addition.js
let x = 5;
let y = 3;
let sum = x + y; // 5 + 3 = 8
console.log(sum); // Output: 8

Subtraction Operator (-)

Here is an example of using the subtraction operator to subtract one number from another:

subtraction.js
let x = 5;
let y = 3;
let difference = x - y; // 5 - 3 = 2
console.log(difference); // Output: 2

Multiplication Operator (*)

Here is an example of using the multiplication operator to multiply two numbers:

multiplication.js
let x = 5;
let y = 3;
let product = x * y; // 5 * 3 = 15
console.log(product); // Output: 15

Division Operator (/)

Here is an example of using the division operator to divide one number by another:

division.js
let x = 6;
let y = 3;
let quotient = x / y; // 6 / 3 = 2
console.log(quotient); // Output: 2

Modulus Operator (%)

Here is an example of using the modulus operator to find the remainder of a division operation:

modulus.js
let x = 7;
let y = 3;
let remainder = x % y; // 7 % 3 = 1
console.log(remainder); // Output: 1

Exponentiation Operator (**)

Here is an example of using the exponentiation operator to raise a number to a power:

exponentiation.js
let x = 2;
let y = 3;
let result = x ** y; // 2^3 = 8
console.log(result); // Output: 8

Increment Operator (++)

The increment operator increases the value of a variable by 1. It can be used as a postfix operator (x++) or a prefix operator (++x). Here is an example of using the increment operator as a postfix operator:

increment-postfix.js
let x = 5;
console.log(x++); // Output: 5

In the example above, the value of x is incremented by 1 using the postfix increment operator (x++).

Here is an example of using the increment operator as a prefix operator:

increment-prefix.js
let x = 5;
console.log(++x); // Output: 6

Although, both forms increase the variable by 1, the difference between the two is that the postfix operator returns the original value of the variable before incrementing it, while the prefix operator returns the value after incrementing it.

Decrement Operator (--)

The decrement operator decreases the value of a variable by 1. It can be used as a postfix operator (x--) or a prefix operator (--x). Here is an example of using the decrement operator as a postfix operator:

decrement-postfix.js
let x = 5;
console.log(x--); // Output: 5

In the example above, the value of x is decremented by 1 using the postfix decrement operator (x--) but in this case because it is a postfix operator, the value of x is returned before decrementing it.

Here is an example of using the decrement operator as a prefix operator:

decrement-prefix.js
let x = 5;
console.log(--x); // Output: 4

In this example, the value of x is decremented by 1 using the prefix decrement operator (--x) and because it is a prefix operator, the value of x is returned after decrementing it.

That's it! You now know how to use arithmetic operators in JavaScript to perform mathematical operations on numbers.

In the next section, we will learn about assignment operators in JavaScript. Let's keep going! 🚀

Made with ❤️ by Fasakin Henry