</>
Skip to content
JavaScript lessons (11/64)

JavaScript — Operators

Arithmetic

let a = 10, b = 3;

a + b    // 13     addition
a - b    // 7      subtraction
a * b    // 30     multiplication
a / b    // 3.333… division (always float-ish)
a % b    // 1      remainder — "what's left over"
a ** b   // 1000   exponent — 10 to the power 3

% is secretly everywhere: even/odd checks (n % 2), cycling through options (i % colors.length), grouping into rows.

Increment and decrement

let n = 5;
n++;    // 6   add one
n--;    // 5   subtract one

The loop staple.

Assignment — including shortcuts

let score = 10;

score += 5;   // 15   same as score = score + 5
score -= 3;   // 12
score *= 2;   // 24
score /= 4;   // 6

Read x += y as "x becomes itself plus y". The long form works identically — shortcuts just save typing once you recognize them.

Comparison — questions with boolean answers

5 === 5     // true   equal value AND type
5 == "5"    // true   equal value after conversion  ⚠️
5 !== 3     // true   not equal (strict)
5 > 3       // true
3 <= 3      // true

The == vs === trap

0 == ""      // true  😱 — JS converts before comparing
0 === ""     // false ✅ — different types, no conversion

Always use === and !==. Modern style guides ban the loose versions outright. Knowing == exists helps you read old code; you never need to write it.

Logical — combining booleans

let age = 22, ticket = true;

age >= 18 && ticket    // true — BOTH must hold (AND)
age < 18 || ticket     // true — at least ONE holds (OR)
!ticket                // false — flips the value (NOT)
ABA && BA || B
truetruetruetrue
truefalsefalsetrue
falsefalsefalsefalse

Real-world shape:

if (user && user.age >= 18) openDoor();

Short-circuit bonus: && stops at the first falsy value (so user.age is only checked if user exists), || returns the first truthy value:

const name = inputName || "Anonymous";

Strings join with +

const first = "Ada", last = "Lovelace";
console.log(first + " " + last);        // Ada Lovelace

// modern template literals — backticks!
console.log(`${first} ${last}`);        // cleaner, multiline-friendly

Template literals (${} inside backticks) replace most concatenation in modern code.

Ternary — if/else as an expression

const status = age >= 18 ? "adult" : "minor";

Reads: condition ? value-if-true : value-if-false. Perfect for picking between two values inline; nested ternaries get unreadable fast.

Operator precedence in one sentence

Multiplication binds tighter than addition, parentheses beat everything:

2 + 3 * 4      // 14, not 20
(2 + 3) * 4    // 20 — when in doubt, parenthesize

Mini Practice

  1. Predict then verify 17 % 5, 2 ** 10, "3" + 3, "3" - 1
  2. Rewrite four x = x op y statements using shortcut operators
  3. Build a login check: username && password && age >= 13
  4. Convert three == comparisons to ===; find which results change
  5. Replace an if/else with a ternary for shipping-cost logic

Next: data types →

Related Topics

Frequently Asked Questions about Operators

What is Operators in JavaScript?

Operators is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Operators?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Operators.

Why is Operators important in JavaScript?

Operators is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.