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.
In JavaScript there are differences in how they function in some particular cases, especially when we compare to strongly typed languages such as Java. Arithmetic operators in JavaScript:
+ | Addition |
– | 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 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" curly brackets 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 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 a 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
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
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 and then assigns the result back to 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 immediatley. 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 the same time.
var a = 1, b = a++; // a is 2 and b is 1 (first the value of a is assigned to be, only afterwards a is incremented) var 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. var 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 as the increment operator, the decrement has two types: pre-decrement and post-decrement.
var a = 1, b = a--; // a is 0 and b is 1 var 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.
var a = "66"; --a // 65 var a = "66"; a = a + 1; // 661