Understanding JavaScript Arithmetic Operators

JavaScript Arithmetic Operators

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: 8

You 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, or NaN depending 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?

JavaScript arithmetic operators are symbols used to perform basic math operations like addition, subtraction, multiplication, and division. They also include special operations like finding remainders (modulus), incrementing or decrementing values, and raising numbers to a power (exponentiation).

How does the addition operator (+) work in JavaScript?

The addition operator (+) in JavaScript adds numbers or joins strings. If both operands are numbers, they are added. If at least one operand is a string, the operator joins the values into one string. For example, "5" + 2 will result in the string "52".

What happens if I divide by zero in JavaScript?

JavaScript does not throw an error when dividing by zero. Instead, it returns Infinity if dividing a positive number by zero, and -Infinity if dividing a negative number by zero. If both values are zero, the result is NaN (Not a Number).

What is the modulus operator (%) in JavaScript?

The modulus operator (%) returns the remainder after division. For example, 10 % 3 will return 1 because 10 divided by 3 leaves a remainder of 1. It is often used for checking even or odd numbers and wrapping values within a fixed range.

How does the exponentiation operator (**) work in JavaScript?

The exponentiation operator (**) raises a number to the power of another number. For example, 2 ** 3 equals 8 (2 raised to the power of 3). It works with both integers and decimals, and supports negative exponents, which give fractional results, like 2 ** -2 which equals 0.25.
Previous Article

JavaScript for...in Loop: Iterating Over Objects

Next Article

PHP File Inclusion: require, include, require_once, include_once

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.