Arithmetic &
Comparison Operators
Master the building blocks of computation in JavaScript: arithmetic operators for math and comparison operators for decision-making.
What Are Operators?
An operator is a special symbol that tells JavaScript to perform a specific action on one or more values. Think of operators as the verbs of programming: they make things happen.
5 + 3, both 5 and 3 are operands.5 + 3 evaluates to 8.Two Families of Operators
Arithmetic operators crunch numbers and return a numeric result. Comparison operators compare values and return true or false.
Why Operators Matter
Almost every line of real-world JavaScript uses an operator. From calculating a shopping cart total to deciding whether a user can vote, operators are the engine of logic and computation.
The Arithmetic Operators
| Operator | Symbol | Example | Result |
|---|---|---|---|
| Addition | + | 7 + 3 | 10 |
| Subtraction | - | 7 - 3 | 4 |
| Multiplication | * | 7 * 3 | 21 |
| Division | / | 7 / 2 | 3.5 |
| Remainder | % | 7 % 3 | 1 |
| Exponentiation | ** | 2 ** 3 | 8 |
Remainder % & Exponent **
let r = 10 % 3; // 1 (10 ÷ 3 = 3 remainder 1)
// Even/Odd check
8 % 2; // 0 → even
7 % 2; // 1 → odd
// Exponentiation: raising to a power
3 ** 4; // 81 (3 × 3 × 3 × 3)
25 ** 0.5; // 5 (square root of 25)
The Comparison Operators
| Operator | Symbol | Example | Result |
|---|---|---|---|
| Strict Equal | === | 5 === 5 | true |
| Strict Not Equal | !== | 5 !== 3 | true |
| Greater Than | > | 7 > 3 | true |
| Less Than | < | 3 < 7 | true |
| Greater or Equal | >= | 5 >= 5 | true |
| Less or Equal | <= | 3 <= 7 | true |
== vs ===: The Critical Difference
5 == "5"; // true (string "5" becomes number 5)
0 == false; // true (false becomes 0)
"" == 0; // true (empty string becomes 0)
// Strict equality (===) — no type conversion
5 === "5"; // false (number ≠ string)
0 === false; // false (number ≠ boolean)
"" === 0; // false (string ≠ number)
Operator Precedence
Just like in math, JavaScript follows an order of operations. Higher-precedence operators execute first.
How to Evaluate an Expression
**, then * / %, then + -.=== to avoid surprises.Example 1: Arithmetic Chain
Example 2: Parentheses Change Everything
// Step 1: (2 + 3) → 5
// Step 2: (4 - 1) → 3
// Step 3: 5 * 3 → 15
Example 3: Mixing Arithmetic & Comparison
let canVote = age + 1 >= 18;
// Step 1: age + 1 → 17 + 1 → 18 (arithmetic first)
// Step 2: 18 >= 18 → true (then comparison)
console.log(canVote); // true
The + Operator with Strings
The + operator does double duty. When both operands are numbers, it adds. When either operand is a string, it concatenates (joins text).
"5" + 3; // "53" (string + number → concatenation)
"Hello" + " World"; // "Hello World"
10 + "px"; // "10px" (number + string → concatenation)
Common Mistakes
"5" == 5 is true, but "5" === 5 is false. Always prefer strict.2 + 3 * 4 and expecting 20 instead of 14. Use parentheses when in doubt."10" + 5 gives "105" (string), not 15. Convert strings to numbers before arithmetic.= is assignment, === is comparison. Writing if (x = 5) assigns 5 to x instead of comparing.Quick Reference Table
| Category | Operators | Returns | Key Rule |
|---|---|---|---|
| Basic Math | + - * / | Number | Standard PEMDAS order |
| Remainder | % | Number | Great for even/odd checks |
| Exponent | ** | Number | Right-to-left associative |
| Equality | === !== | Boolean | Always use strict versions |
| Relational | > < >= <= | Boolean | Works on numbers and strings |
Question 1 of 5
What is the value of 4 + 3 * 2 in JavaScript?
3 * 2 = 6 first, then 4 + 6 = 10.Question 2 of 5
What does "5" === 5 evaluate to?
=== checks both value and type. "5" is a string and 5 is a number: different types means false.Question 3 of 5
What is the result of 17 % 5?
% operator returns the remainder after integer division. \( 17 \div 5 = 3 \) with remainder \( 2 \), so 17 % 5 = 2.Question 4 of 5
What does "10" + 5 produce in JavaScript?
+ operator performs concatenation instead of addition. The number 5 is converted to the string "5", giving "10" + "5" = "105".Question 5 of 5
Which expression correctly checks if a variable score is greater than or equal to 90?
>= is the "greater than or equal to" operator. Option A (=) is for assignment. Option B (==) checks for loose equality and misses the "greater than" condition. Option D (=>) is arrow function syntax.