On this page
Math Operators in JavaScript
JavaScript provides a variety of operators for performing mathematical operations. These operators can be used to perform arithmetic, assign values, compare values, and more. Here are some of the most commonly used math operators in JavaScript:
Arithmetic Operators
- Addition (+): Adds two numbers.
let sum = 10 + 5;
console.log(sum); // 15
- Subtraction (-): Subtracts one number from another.
let difference = 10 - 5;
console.log(difference); // 5
- Multiplication (*): Multiplies two numbers.
let product = 10 * 5;
console.log(product); // 50
- Division (/): Divides one number by another.
let quotient = 10 / 5;
console.log(quotient); // 2
- Modulus (%): Returns the remainder of a division operation.
let remainder = 10 % 3;
console.log(remainder); // 1
- Exponentiation ()**: Raises a number to the power of another number.
let power = 2 ** 3;
console.log(power); // 8
Increment and Decrement Operators
- Increment (++): Increases a number by one.
let a = 5;
a++;
console.log(a); // 6
- Decrement (–): Decreases a number by one.
let b = 5;
b--;
console.log(b); // 4
Assignment Operators
- Assignment (=): Assigns a value to a variable.
let x = 10;
console.log(x); // 10
- Addition Assignment (+=): Adds a value to a variable and assigns the result to the variable.
let x = 10;
x += 5;
console.log(x); // 15
- Subtraction Assignment (-=): Subtracts a value from a variable and assigns the result to the variable.
let x = 10;
x -= 5;
console.log(x); // 5
- Multiplication Assignment (*=): Multiplies a variable by a value and assigns the result to the variable.
let x = 10;
x *= 5;
console.log(x); // 50
- Division Assignment (/=): Divides a variable by a value and assigns the result to the variable.
let x = 10;
x /= 5;
console.log(x); // 2
- Modulus Assignment (%=): Takes the modulus of a variable and a value and assigns the result to the variable.
let x = 10;
x %= 3;
console.log(x); // 1
- Exponentiation Assignment (=)**: Raises a variable to the power of a value and assigns the result to the variable.
let x = 2;
x **= 3;
console.log(x); // 8
Comparison Operators
- Equal (==): Checks if two values are equal.
console.log(5 == "5"); // true
- Strict Equal (===): Checks if two values are equal and of the same type.
console.log(5 === "5"); // false
- Not Equal (!=): Checks if two values are not equal.
console.log(5 != "5"); // false
- Strict Not Equal (!==): Checks if two values are not equal or not of the same type.
console.log(5 !== "5"); // true
- Greater Than (>): Checks if the left value is greater than the right value.
console.log(10 > 5); // true
- Greater Than or Equal (>=): Checks if the left value is greater than or equal to the right value.
console.log(10 >= 5); // true
- Less Than (<): Checks if the left value is less than the right value.
console.log(5 < 10); // true
- Less Than or Equal (<=): Checks if the left value is less than or equal to the right value.
console.log(5 <= 10); // true
Logical Operators
- Logical AND (&&): Returns the second value if the first is truthy; otherwise returns the first value.
console.log(true && true); // true
console.log(true && false); // false
console.log(5 && 10); // 10
- Logical OR (||): Returns the first truthy value, or the last value if all are falsy.
console.log(false || true); // true
console.log(0 || 42); // 42
- Logical NOT (!): Converts a value to its boolean opposite.
console.log(!true); // false
console.log(!0); // true
- Nullish Coalescing (??): Returns the right-hand value only when the left is
nullorundefined.
console.log(null ?? 'default'); // 'default'
console.log(0 ?? 'default'); // 0 (0 is not nullish)
Bitwise Operators
Bitwise operators work on 32-bit integer representations:
console.log(5 & 1); // 1 (AND)
console.log(5 | 1); // 5 (OR)
console.log(5 ^ 1); // 4 (XOR)
console.log(~5); // -6 (NOT)
console.log(5 << 1); // 10 (left shift)
console.log(5 >> 1); // 2 (right shift)
Ternary Operator
A shorthand for simple if-else expressions:
let age = 20;
let status = age >= 18 ? 'adult' : 'minor';
console.log(status); // 'adult'
Operator Precedence
Operators are evaluated in a defined order. Use parentheses () when in doubt:
console.log(2 + 3 * 4); // 14 (multiplication first)
console.log((2 + 3) * 4); // 20
JavaScript provides a wide range of math operators for performing arithmetic, assignment, and comparison operations.
Advanced Examples
- Calculating the Area of a Circle:
let radius = 5;
let area = Math.PI * radius ** 2;
console.log(area); // 78.53981633974483
- Swapping Values Using Destructuring:
let a = 3;
let b = 5;
[a, b] = [b, a];
console.log(a, b); // 5 3
- Using Modulus to Determine Even or Odd:
let num = 10;
if (num % 2 === 0) {
console.log(`${num} is even.`);
} else {
console.log(`${num} is odd.`);
}
By mastering these operators, you can perform a wide range of mathematical and logical operations in your JvaScript programs.