Course Content
Introduction to JavaScript
Introduction to JavaScript
Mathematical Operations
JavaScript can perform the following operations with numbers:
- Addition (
+
); - Subtraction (
-
); - Multiplication (
*
); - Division (
/
); - Remainder, or Modulo (
%
); - Exponent (
**
).
Note
If you are already familiar with these operations and how they work, skip to the last section (Priority of Execution of Operations) or proceed with this chapter.
Addition and Subtraction
console.log(25 + 13); // Addition console.log(37 - 2); // Subtraction let a = 25, b = 23; console.log(a + b); // Addition console.log(a - b); // Subtraction
Multiplication and Division
console.log(12 * 3); // Multiplication console.log(12 / 3); // Division console.log(273 / 23); // Division let a = 77, b = 11; console.log(a * b); // Multiplication console.log(a / b); // Division
Remainder (Modulo)
This operation returns the remainder of a division and is performed using the %
operator:
console.log(77 % 10); console.log(25 % 11); let a = 27, b = 21; console.log(a % b);
Exponent
This operation raises a number to a certain power. The first number is the base, and the second is the exponent to which it must be raised. It is performed using the **
operator:
console.log(10 ** 6); // 10 * 10 * 10 * 10 * 10 * 10 (6 times) console.log(2 ** 7); // 2 * 2 * 2 * 2 * 2 * 2 * 2 (7 times) let a = 2; let b = 3; console.log(a ** b);
Priority of Execution of Operations
Each operation has its execution priority, and the sequence of execution depends on it.
Note
If operations have the same priority, they will be executed from left to right.
You can use parentheses ( )
to modify the priority of execution:
console.log(25 + 7 * 2 ** 3); // Example 1 console.log((25 + 7) * 2 ** 3); // Example 2 console.log(((25 + 7) * 2) ** 3); // Example 3
Note
Parentheses
()
have the highest priority. Inner parentheses are evaluated first, followed by outer ones.
Thanks for your feedback!