In our daily lives, we perform arithmetic operations to help us with various calculations, such as assessing the cost of our purchases, determining the dimensions of a room or garden area, estimating the number of guests invited to a party, and so on. Similarly, programmers also need to use mathematical operations while writing programs.
Binary arithmetic operators
The JavaScript programming language provides operators to perform arithmetic operations. They are called binary operators because they apply to two operands (objects over which the operation is completed).
Addition
+
console.log(12 + 26); // 38
console.log(5 + 4.5); // 9.5Usually,
+adds numbers, but if you use this operator with strings, it combines them into one:
console.log("My name is " + "John"); // My name is JohnSubtraction
-
console.log(5 - 3); // 2
console.log(6 - 0.1); // 5.9Multiplication
*
console.log(10 * 4); // 40
console.log(2 * 1.5); // 3Division
/
console.log(8 / 2); // 4
console.log(12 / 5); // 2.4Remainder
%. This operator finds the remainder of the division:
console.log(10 % 3); // 1, because 10 divided by 3 leaves a remainder of 1
console.log(12 % 4); // 0, because 12 divided by 4 leaves no remainderExponentiation
**
console.log(2 ** 3); // 8, because 2 * 2 * 2 is 8Writing complex expressions
Arithmetic operations can be combined to write more complex expressions:
console.log(1 + 3 * 4 - 2) // 11The calculation order follows the basic rules of arithmetic operations. Multiplication has a higher priority level than addition and subtraction, so 3 * 4 is calculated first.
To specify the order of execution, we can use parentheses, as in the following example:
console.log((1 + 3) * (4 - 2)); // 8As in arithmetic, parentheses can be nested. Feel free to use them for better code clarity.
Unary operators
Unary operators apply to a single operand.
The unary plus operator indicates a positive value. It's an optional operator if you only work with numbers:
console.log(+7); // 7The unary minus operator makes a value or an expression negative:
console.log(-9); // -9
console.log(-(100 + 5)); // -105Operator precedence
If several operators are involved in an expression, they will be executed in the order of priority. The list below is sorted from the highest to the lowest precedence level:
Parentheses
Unary plus/minus
Exponentiation
Multiplication and division
Addition and subtraction
The order of performing arithmetic operations in JavaScript is also commonly used in mathematics.
Conclusion
Arithmetic operations are an essential part of programming and are frequently used in coding. JavaScript provides a range of arithmetic operators that can be used to perform various calculations on numbers and strings:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Remainder (%)
Exponentiation (**)
The execution order for these operators follows the same rules as commonly used in mathematics. Use parentheses to control the execution order. By mastering the arithmetic operators, programmers can write more complex expressions and develop more sophisticated programs.