Updated date:

JavaScript Arithmetic Operators

I'm a software developer with a great interest in data analysis and statistics.

javascript-arithmetic-operators

Arithmetic operators are used to perform basic arithmetic on numbers (integers, floating points, etc.). They also perform some actions or manipulation on strings. The operators are mostly the same as in many other programming languages. More on data types in JavaScript.

In JavaScript, there are differences in how arithmetic operators function in some particular cases, especially when we compare to strongly typed languages such as Java. JavaScript Arithmetic operators:

JavaScript Arithmetic Operators

SymbolMeaning

+

Addiction

-

Subtraction

*

Multiplication

/

Division

%

Modulo or remainder

++

Increment

--

Decrement

Addition Operator +

The addition operator adds numbers or concatenates strings, depending on the datatypes of the operands.

2 + 3 // 5
"Hi, " + "you" // "Hi, you"
"2" + "3" // "23"

As you’ve seen in the example above, when the values of both operands are numbers it will be basic arithmetic addition. When both are strings, then it’s also pretty much obvious what the + operator does, namely gluing the operands together, also known as concatenation.

In many other cases, type conversion is needed and the result depends on the position of the operands. The addition operator will set the priority to string concatenation. If one of the operands is a string, then the result will be a string.

If you perform addition on a pair of operands that are of the number type and then you add a string, it will first perform the arithmetic on the numbers and next it will concatenate with the String (“3 eyes” example below).

If you have a number type with the addition operator followed by a string, another addition operator and then a number again, then it will concatenate the first number with the string, then concatenate that result with the last number (“1 eyes 2” example below).

"2" + 3 // "23" concatenation
2 + "3" // "23" concatenation
1 + 2 + " eyes" // "3 eyes" the two numbers add and then concatenation
1 + " eyes " + 2 // "1 eyes 2"
1 + ( 2 + " eyes") // "12 eyes" parentheses define priority

Subtraction Operator –

The subtraction operator works like normal arithmetic. If you subtract and one or more of your operands are strings of non-numeric characters you’ll get NaN (Not a Number) as a result.

If you subtract two strings with only numeric characters, you’ll get a number, like normal subtraction of numbers. If one operand is a string with only numeric characters and the other operand is a number you’ll get a number as a result.

2 - 3 // -1
"2" - "3" // -1
6 - "4" // 2
"6" + "3" - "2" // 61 first concatenation then substraction

Multiplication Operator *

It takes two operands and multiplies them, just like basic math. If both operands are numerical strings, they will be coerced to numbers (type conversion) and the result will be multiplication of those numbers. The same thing will happen if one operand is of type number and the other is a numerical string.

If one or more operands are non-numerical (words, alphabetical chars,…) strings we get NaN (Not a Number).

2 * 3 // 6
"2" * 3 // 6
"2" * "3" // 6
2 * "P" // NaN

Division Operator /

The division operator takes number operands and will return a number, float or “Infinity” (if the later operand is zero) after dividing them. Type conversion will occur for numerical strings. For non-numerical string, we will get NaN, similar to multiplication and subtraction.

2 / 3 // 0.66666666666666
"2" / 3 // 0.66666666666666
"2" / 3 // 0.66666666666666
2 / 0 // Infinity
2 / "text" // NaN

Modulo Or Remainder Operator %

This special modulo operator finds the remainder after division of one number of another. Here we also have type conversion for numerical strings and NaN when non-numerical strings are involved.

2 % 3 // 2
3 % 2 // 1
"2" % 3 // 2

Increment Operator ++

The increment operator adds 1 to its single operand. The operator will add 1 to the value of the variable (let) and then assigns the result back to the variable.

There are two types of increment operators: pre-increment and post-increment operators. Pre-increment will increment the operand and evaluates to the incremented value immediately. While post-increment will increment its operand but will still evaluate to the unincremented value of that operand when assigned to another variable at the same time.

let a = 1, b = a++;  // a is 2 and b is 1 (first the value of a is assigned to b, only afterwards a is incremented)
let a = 1, b = ++a;  // a and b are 2
   
9-- // will throw an uncaught reference error, so the value has to be assigned to a variable.
   
let x = "test";
x++ // NaN   Not a Number

Decrement Operator – –

The decrement operator will convert the value of the operand to a number (if needed and possible), subtract 1, and assign the result into the operand (variable). Similar to the increment operator, the decrement has two types: pre-decrement and post-decrement.

let a = 1, b = a--;  // a is 0 and b is 1
let a = 1, b = --a;  // a and b are 0

It is worth to note that the expression ++a is not always the same as a = a + 1. The increment operator and decrement operator always convert the operand first.

let a = "66";
--a //  65
   
let a = "66";
a = a + 1; // 661

© 2019 Sam Shepards

Related Articles