Table of Content
JavaScript arithmetic operators let you add, subtract, multiply, or divide numbers. You can also use them to find remainders or increase values.
What are JavaScript Arithmetic Operators
JavaScript arithmetic operators are symbols that let you do math with numbers. They include basic operations like addition (+), subtraction (-), multiplication (*), and division (/).
Here are the basic operators:
- Remainder when you use modulus (
%) - Increment (
++) to add 1 - Decrement (
--) to subtract 1 - Division (
/) to divide 1 - Exponentiation (
**) to raise a number to a power
Let’s take a look at each one in depth.
The Addition Operator (+) in JavaScript
The addition operator (+) in JavaScript adds numbers. It also joins strings. What it does depends on the values you use with it.
When both values are numbers, + adds them.
let sum = 5 + 3;
console.log(sum);
// Output: 8You can also use variables:
let a = 10;
let b = 4;
let result = a + b;
console.log(result);Output:
14
If at least one value is a string, + joins them into one string.
let text = "Hello" + " " + "FlatCoding Students!";
console.log(text); Output:
Hello FlatCoding Students!
It works with numbers and strings:
let mix = "The answer is " + 42;
console.log(mix);Output:
The answer is 42
If one value is a string, JavaScript turns the other into a string, no matter the order.
let example = 7 + " apples";
console.log(example);Output:
7 apples
JavaScript follows type rules. If one value is a string, it turns the other into a string too.
let value = 2 + "2";
console.log(value);Output:
22
You can combine multiple values:
let result = 2 + 3 + "7"; JavaScript runs left to right. So 2 + 3 = 5, then 5 + "7" = "57".
Subtraction Operator (-) in JavaScript
The subtraction operator (-) in JavaScript takes one number away from another. It only works with numbers or values that can turn into numbers.
You can subtract two numbers directly.
let result = 10 - 4;
console.log(result);Output:
6
You can also use variables:
let a = 20;
let b = 5;
let difference = a - b;
console.log(difference);Output:
15
If you subtract values that are not numbers, JavaScript tries to convert them.
let result = "10" - "3";
let fail = "ten" - 2;
console.log({result, fail});Output:
{ result: 7, fail: NaN }
The - operator never joins strings. It always tries to do math. If it cannot, the result is NaN.
let value = "5" - 1;
let broken = "hi" - 1;
console.log(value);
console.log(broken);Output:
4
NaN
Multiplication Operator (*) in JavaScript
You use the multiplication operator (*) to multiply two numbers. It also tries to turn other types into numbers if needed.
You can multiply any two numbers:
let result = 6 * 3;
console.log(result);Output:
18
With variables:
let a = 4;
let b = 2.5;
let total = a * b;
console.log(total);Output:
10
If the value looks like a number, JavaScript converts it:
let product = "5" * "2";
let error = "five" * 2;
console.log(product);
console.log(error);Output:
10
NaN
Booleans also work:
let result = true * 10;
let result2 = false * 10;
console.log(result);
console.log(result2);Output:
10
0
Zero always gives zero:
let result = 10 * 0;
console.log(result); Output:
0
Negative numbers flip the sign:
let result = -3 * 4;
console.log(result); Output:
-12
Division Operator (/) in JavaScript
The division operator (/) in JavaScript divides one number by another. It follows the usual math rules but handles some edge cases differently.
You can divide numbers directly:
let result = 12 / 4;
console.log(result); Output:
3
You can also divide variables:
let a = 20;
let b = 5;
let answer = a / b;
console.log(answer)Output:
4
If the numbers do not divide evenly, you get a decimal:
let result = 7 / 2;
console.log(result);Output:
3.5
JavaScript tries to turn strings and other types into numbers:
let value = "10" / "2";
let fail = "ten" / 2;
console.log(value);
console.log(fail);Output:
5
NaN
Booleans also convert:
let result = true / 2;
let result2 = false / 2;
console.log(result);
console.log(result2);Output:
0.5
0
JavaScript does not crash when you divide by zero. It returns Infinity or -Infinity:
let x = 5 / 0;
let y = -5 / 0;
console.log(x);
console.log(y);Output:
Infinity
-Infinity
If both values are zero, you get NaN:
let z = 0 / 0;
console.log(z);Output:
NaN
Modulus Operator (%) in JavaScript
The modulus operator (%) in JavaScript gives the remainder after division. It works with whole numbers and decimals.
Use % when you want what is left over:
let result = 10 % 3;
console.log(result);Output:
1
Another example:
let value = 15 % 4;
console.log(value);Output:
3
If the number divides evenly, the result is 0:
let result = 20 % 5;
console.log(result);Output:
0
The sign of the result follows the first number:
let x = -7 % 3;
let y = 7 % -3;
let z = -7 % -3;
console.log(x, y, z);Output:
-1 1 -1
You can use % to:
- Check if a number is even:
let isEven = (num % 2 === 0); - Wrap values in a fixed range:
let index = i % array.length;
Exponentiation Operator (**) in JavaScript
The exponentiation operator (**) in JavaScript raises a number to the power of another number. It works like Math.pow() but uses a simpler syntax.
The number on the left is the base. The number on the right is the exponent.
let result = 2 ** 3;
console.log(result)Output:
8
You can use it the same way with variables:
let base = 5;
let power = 2;
let answer = base ** power;
console.log(answer);Output:
25
You can use decimal exponents:
let result = 9 ** 0.5;
console.log(result);output:
3
Negative exponents give fractions:
let value = 2 ** -2;
console.log(value)Output:
0.25
Exponentiation works right to left:
let result = 2 ** 3 ** 2;
console.log(result);Output:
512
Wrapping Up
You learned how JavaScript uses arithmetic operators to handle basic math and value changes. You saw how each one works with numbers, strings, booleans, and special cases.
Here is a quick recap:
- Addition (
+) adds numbers or joins strings. - Subtraction (
-) removes one number from another and only works with values that can turn into numbers. - Multiplication (
*) multiplies numbers and handles conversion for strings and booleans. - Division (
/) divides values and returns decimals,Infinity, orNaNdepending on the input. - Modulus (
%) gives the remainder after division and helps with range checks. - Exponentiation (
**) raises a number to a power, including decimal and negative exponents.
Thank you for reading. Click to see more JavaScript tutorials.
FAQ’s
What are JavaScript arithmetic operators?
How does the addition operator (+) work in JavaScript?
What happens if I divide by zero in JavaScript?
What is the modulus operator (%) in JavaScript?
How does the exponentiation operator (**) work in JavaScript?
Similar Reads
JavaScript object methods are simple ways to handle data inside objects. An object can hold many values, and methods give…
The forEach loop in JavaScript allows you to go through each item in an array. You do not have to…
JavaScript toReversed returns a new array with the reversed order of its elements. It does not change the original array…
Object References in JavaScript mean that variables do not store full objects but only their references in memory. What is…
If you are a coder, one of your primary requirements is to have a trustworthy code- editor. A Code editor…
JavaScript includes Math.sign to find out if a number is positive. It also helps detect negative values or zero. It…
In this guide, you will learn how JavaScript functions work and why they matter. Each section shows what you need…
Some numbers come from user input and other come from math operations. Sometimes, these numbers need to stay positive. The…
If you are ready to take your first steps into the realm of web development, then there cannot be a…
Logical operators in JavaScript let you check conditions and combine multiple checks in your code. In this guide, you will…