01 / 21
</> + = =

Arithmetic &
Comparison Operators

Master the building blocks of computation in JavaScript: arithmetic operators for math and comparison operators for decision-making.

JavaScript

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.

Operands
The values an operator acts on. In 5 + 3, both 5 and 3 are operands.
Expressions
A combination of operands and operators that evaluates to a single value: 5 + 3 evaluates to 8.

Two Families of Operators

Arithmetic Produce numbers + - * / % **

e.g. \(10+5 \to 15\)

Comparison Produce booleans === !== > < >= <=

e.g. \(10 > 5 \to \text{true}\)

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.

Calculations
Compute totals, averages, discounts, scores, and physics simulations.
Decisions
Power if/else blocks, form validation, access control, and game rules.
Data Flow
Filter arrays, sort lists, and control loops that repeat actions.

The Arithmetic Operators

Operator Symbol Example Result
Addition+7 + 310
Subtraction-7 - 34
Multiplication*7 * 321
Division/7 / 23.5
Remainder%7 % 31
Exponentiation**2 ** 38

Remainder % & Exponent **

// Remainder: "what's left over?"
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 === 5true
Strict Not Equal!==5 !== 3true
Greater Than>7 > 3true
Less Than<3 < 7true
Greater or Equal>=5 >= 5true
Less or Equal<=3 <= 7true

== vs ===: The Critical Difference

// Loose equality (==) — converts types first
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.

1
Parentheses ( ): always evaluated first
2
Exponentiation **
3
Multiplication, Division, Remainder * / %
4
Addition, Subtraction + -
5
Comparisons > < >= <= === !==

How to Evaluate an Expression

1
Identify all operators in the expression and note their precedence levels.
2
Resolve parentheses first: evaluate any grouped sub-expressions from the innermost outward.
3
Apply higher-precedence operators next: **, then * / %, then + -.
4
Apply comparisons last: the arithmetic result becomes one side of the comparison.
5
Check types: Are you comparing number to number? Use === to avoid surprises.

Example 1: Arithmetic Chain

let result = 2 + 3 * 4 - 1;
1
Multiplication first: \(3 \times 4 = 12\)
2
Left to right: \(2 + 12 = 14\)
3
Then subtraction: \(14 - 1 = 13\)

Example 2: Parentheses Change Everything

let a = (2 + 3) * (4 - 1);
// Step 1: (2 + 3) → 5
// Step 2: (4 - 1) → 3
// Step 3: 5 * 3 → 15
Without parens
\(2 + 3 \times 4 - 1 = 13\)
With parens
\((2 + 3) \times (4 - 1) = 15\)

Example 3: Mixing Arithmetic & Comparison

let age = 17;
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; // 8 (number + number → addition)
"5" + 3; // "53" (string + number → concatenation)
"Hello" + " World"; // "Hello World"
10 + "px"; // "10px" (number + string → concatenation)

Common Mistakes

✕ Using == instead of ===
Loose equality coerces types silently. "5" == 5 is true, but "5" === 5 is false. Always prefer strict.
✕ Forgetting precedence
Writing 2 + 3 * 4 and expecting 20 instead of 14. Use parentheses when in doubt.
✕ String + Number trap
"10" + 5 gives "105" (string), not 15. Convert strings to numbers before arithmetic.
✕ Confusing = with ===
= 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?

Question 2 of 5

What does "5" === 5 evaluate to?

Question 3 of 5

What is the result of 17 % 5?

Question 4 of 5

What does "10" + 5 produce in JavaScript?

Question 5 of 5

Which expression correctly checks if a variable score is greater than or equal to 90?