Arithmetic Operators are used to perform arithmetic operations on their operands.
*) operator is used to multipy two numbers.
console.log(2 * 3); // 6
Since, the multiplication operator can be used only with numbers, any non-numeric operand results in Not a Number(
console.log(2 * "A"); // NaN
The forward slash(
/) operator is used to divide two numbers.
console.log(6 / 2); // 3
console.log(5 / 3); // 1.6666666666666667
The division operator works only with numbers. If any of the operands is a non-numeric value, the result results in
console.log(5 / "A"); // NaN
-Infinity. When we try to divide any number by
0, the result is
-Infinity based on the numerator.
console.log(1 / 0); // Infinity console.log(-1 / 0); // -Infinity
If the numerator is
0, the result of division is always
console.log(0 / 4); // 0
What happens when we divide a
0? Will the answer be
NaN, if we try to divide
console.log(0 / 0); // NaN
console.log(4 / "2"); // 2 console.log(4 / "A"); // NaN
In order to find the remainder of a division operation, we can use modulo(
console.log(10 % 4); // 2
The sign of the result is the sign of the first operand. The remainder will be negative if the first operand is negative. The remainder will be positive integer, if the first operand is positive.
console.log(5 % 2); // 1 console.log(-5 % 2); // -1 console.log(4 % 2); // 0
console.log(5.1 % 2.3); // 0.5
The subtraction operator(
-) is used to find the difference between two numbers.
console.log(5 - 3); // 2
Subtraction operator works only with numbers. So if any of the operands cannot be converted to a number, the result will be
console.log(5 - "A"); // NaN
+ is used to add two numbers. The operator
+ is overloaded. That means, it behaves differently based on the type of operands. If both the operands are numbers,
+ performs addition.
console.log(3 + 4); // 7
If any of the operands is not a number, then the result is calculated based on following rules.
If both the operands are strings, it performs concatenation.
console.log("Apple" + "Banana"); // "AppleBanana"
In any other case, type conversion is required.
+ operator gives more priority to string concatenation. If either of the operands is a string or an object that can be converted to a string, all operands are converted to string and string concatenation is performed. In all other cases, that is if both operands are not strings or can be converted to strings, addition operation is performed.
Let us consider different cases.
If one of the operand is a string, other operands are also converted to string and string concatenation is done.
console.log("A" + 2); // "A2"
If one of the operand is an object, that can be converted to a string, the string concatenation occurs.
console.log([2, 3] + 5); // "2,35"
If none of the operands can be converted to a string, addition occurs.
undefined cannot be converted to a string.
console.log(3 + null); // 0 console.log(3 + undefined); // NaN
When addition is done, all operands are converted to numbers.
null converts to 0 and
undefined converts to
+ operator has left-to-right associativity. Consider the following statement.
console.log(2 + 4 + "Hello"); // "6Hello" console.log(2 + (4 + "Hello")); // "24Hello"
First line first performs number addition of
4. The result
6 is then concatenated with
"Hello". Whereas in the second line the default associativity is altered using paranthesis.