JavaScript Arithmetic
JavaScript Arithmetic
Section titled “JavaScript Arithmetic”Performing arithmetic operations is a fundamental aspect of programming, and JavaScript provides a standard set of operators for this purpose.
JavaScript Arithmetic Operators
Section titled “JavaScript Arithmetic Operators”Arithmetic operators are used to perform mathematical calculations on numbers (which can be literals or variables).
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (Remainder) |
| ** | Exponentiation (ES2016) |
| ++ | Increment |
| — | Decrement |
Arithmetic Operations
Section titled “Arithmetic Operations”A typical arithmetic operation involves one or two numbers.
These numbers can be literals:
let sum = 100 + 50; // sum will be 150or variables:
let a = 10;let b = 5;let difference = a - b; // difference will be 5or expressions:
let result = (100 + 50) * 3; // result will be 450Operators and Operands
Section titled “Operators and Operands”In an arithmetic operation, the numbers are called operands. The operation itself (like addition or multiplication) is defined by an operator.
| Operand | Operator | Operand |
|---|---|---|
| 100 | + | 50 |
The addition operator (+) adds numbers:
let x = 5;let y = 2;let z = x + y; // z is 7The subtraction operator (-) subtracts numbers:
let x = 5;let y = 2;let z = x - y; // z is 3The multiplication operator (*) multiplies numbers:
let x = 5;let y = 2;let z = x * y; // z is 10The division operator (/) divides numbers:
let x = 5;let y = 2;let z = x / y; // z is 2.5The modulus operator (%) returns the division remainder:
let x = 5;let y = 2;let z = x % y; // z is 1 (5 divided by 2 is 2 with a remainder of 1)The exponentiation operator (**) raises the first operand to the power of the second operand (ES2016):
let x = 5;let y = 2;let z = x ** y; // z is 25 (5 to the power of 2)The increment operator (++) increases a number by 1. It can be used as a prefix (++x) or postfix (x++).
let x = 5;x++; // x is now 6 (postfix increment)let z = x; // z is 6
let y = 5;let w = ++y; // y is 6, w is 6 (prefix increment, increments y then assigns to w)The decrement operator (--) decreases a number by 1. It can also be prefix or postfix.
let x = 5;x--; // x is now 4 (postfix decrement)let z = x; // z is 4
let y = 5;let w = --y; // y is 4, w is 4 (prefix decrement)Note: JavaScript numbers are 64-bit floating-point numbers. This can sometimes lead to precision issues with decimal arithmetic. For high-precision arithmetic, especially financial, consider using libraries or BigInt for whole numbers.
Operator Precedence
Section titled “Operator Precedence”Operator precedence dictates the order in which operations are performed in an arithmetic expression. For example:
let x = 100 + 50 * 3;Is the result (100 + 50) * 3 = 450, or 100 + (50 * 3) = 250?
Multiplication and division have higher precedence than addition and subtraction. So, 50 * 3 is performed first.
Result: 100 + 150 = 250.
As in standard mathematics, parentheses () can be used to override the default precedence and force an operation to be performed first:
let x = (100 + 50) * 3; // x is 450When operators have the same precedence (e.g., addition and subtraction, or multiplication and division), they are typically evaluated from left to right.
let x = 100 + 50 - 3; // (100 + 50) then - 3. Result is 147.Arithmetic Operator Precedence Table
Section titled “Arithmetic Operator Precedence Table”The following table lists JavaScript arithmetic operators, ordered from highest to lowest precedence:
| Operator | Description (Precedence Level) |
|---|---|
| ( ) | Grouping (Expression grouping) |
| ++ — | Increment (postfix x++), Decrement (postfix x--) |
| ++ — | Increment (prefix ++x), Decrement (prefix --x), Unary Plus (+x), Unary Negation (-x) |
| ** | Exponentiation |
| * / % | Multiplication, Division, Modulus |
| + - | Addition, Subtraction |
Note: This table shows common arithmetic operators. For a full list of operator precedence, refer to the MDN documentation. Expressions in parentheses are always fully computed before their value is used in the rest of the expression.
Common learning obstacle: Forgetting precedence rules. When in doubt, use parentheses () to make the order of operations explicit and improve code readability.