DocsHub
Basics

JavaScript Operators

Learn how to work with values using arithmetic, comparison, logical, and assignment operators.

JavaScript Operators

An operator is a symbol that performs an action on values.

let total = 10 + 5; // + is the operator
console.log(total); // 15

The values an operator works on are called operands. JavaScript has several categories of operators — each with a specific job.


1. Arithmetic Operators

These do math.

let a = 10;
let b = 3;

console.log(a + b);  // 13 — addition
console.log(a - b);  // 7  — subtraction
console.log(a * b);  // 30 — multiplication
console.log(a / b);  // 3.3333... — division
console.log(a % b);  // 1  — remainder (modulo)
console.log(a ** b); // 1000 — exponentiation (10 to the power of 3)

Modulo % — the underrated one

Modulo gives you the remainder after division. It looks simple but it's incredibly useful.

console.log(10 % 2); // 0 — 10 divides evenly, no remainder
console.log(11 % 2); // 1 — 11 is odd, remainder is 1
console.log(15 % 4); // 3 — 4 goes into 15 three times (12), remainder 3

A classic use — checking if a number is even or odd:

let number = 7;

if (number % 2 === 0) {
  console.log("Even");
} else {
  console.log("Odd"); // Odd
}

2. Assignment Operators

These store values into variables. You already know = — but there are shortcuts.

let score = 10;

score += 5;  // same as: score = score + 5  → 15
score -= 3;  // same as: score = score - 3  → 12
score *= 2;  // same as: score = score * 2  → 24
score /= 4;  // same as: score = score / 4  → 6
score **= 2; // same as: score = score ** 2 → 36
score %= 5;  // same as: score = score % 5  → 1

These are just shorthand — they keep your code cleaner.


3. Comparison Operators

These compare two values and always return true or false.

let age = 20;

console.log(age > 18);   // true  — greater than
console.log(age < 18);   // false — less than
console.log(age >= 20);  // true  — greater than or equal
console.log(age <= 19);  // false — less than or equal
console.log(age === 20); // true  — strictly equal
console.log(age !== 20); // false — strictly not equal

== vs === — the most important distinction

This is where many beginners make mistakes.

== checks value only — it converts types before comparing. === checks value AND type — no conversion, strict.

console.log(5 == "5");   // true  — JS converts "5" to 5, then compares
console.log(5 === "5");  // false — number vs string, different types

console.log(0 == false);  // true  — JS converts false to 0
console.log(0 === false); // false — number vs boolean

console.log(null == undefined);  // true  — special JS rule
console.log(null === undefined); // false — different types

== does silent type conversion — called type coercion — and it produces surprising results.

Always use === unless you have a specific reason not to. It does exactly what you expect and nothing more.


4. Logical Operators

These combine or reverse boolean values. You'll use these constantly in conditions.

&& — AND

Returns true only if both sides are true.

let age = 20;
let hasID = true;

console.log(age >= 18 && hasID === true); // true — both conditions met
console.log(age >= 18 && hasID === false); // false — one condition failed

|| — OR

Returns true if at least one side is true.

let isWeekend = false;
let isHoliday = true;

console.log(isWeekend || isHoliday); // true — one is enough
console.log(isWeekend || false);     // false — both are false

! — NOT

Flips the boolean — true becomes false, false becomes true.

let isLoggedIn = false;

console.log(!isLoggedIn);  // true
console.log(!true);        // false

A real example combining all three:

let age = 25;
let hasMembership = true;
let isBanned = false;

if (age >= 18 && hasMembership && !isBanned) {
  console.log("Access granted");  // Access granted
}

5. Increment and Decrement

A shortcut to add or subtract 1.

let count = 0;

count++; // same as count = count + 1
console.log(count); // 1

count--; // same as count = count - 1
console.log(count); // 0

Prefix vs Postfix

The position of ++ matters when you use the value in the same line.

let a = 5;
console.log(a++); // 5 — returns value first, then increments
console.log(a);   // 6

let b = 5;
console.log(++b); // 6 — increments first, then returns value
console.log(b);   // 6

In most real code you'll use count++ on its own line — so this distinction rarely matters. But you will see it in code written by others.


6. String Operator

+ works on strings too — it joins them together. This is called concatenation.

let firstName = "Ali";
let lastName = "Khan";

console.log(firstName + " " + lastName); // Ali Khan

But if you mix a string and a number, + leans toward string:

console.log("Score: " + 100);  // "Score: 100" — number becomes string
console.log(10 + 20 + "px");   // "30px" — adds numbers first, then joins string
console.log("px" + 10 + 20);   // "px1020" — string first, so rest gets joined

This behavior is type coercion — we cover it in full detail in the next topic.


7. Ternary Operator

The ternary operator is a one-line if/else. It takes three parts — a condition, a value if true, and a value if false.

condition ? valueIfTrue : valueIfFalse
let age = 20;
let access = age >= 18 ? "Allowed" : "Denied";
console.log(access); // Allowed

Same thing written as a full if/else:

let access;
if (age >= 18) {
  access = "Allowed";
} else {
  access = "Denied";
}

Use the ternary when the logic is simple and fits cleanly on one line. Don't nest them — it gets unreadable fast.


Operator Precedence

When multiple operators appear in one expression, JavaScript follows a specific order — just like math. Multiplication before addition, and so on.

console.log(2 + 3 * 4);    // 14 — not 20. * runs before +
console.log((2 + 3) * 4);  // 20 — parentheses force + first

You don't need to memorize the full precedence table. Just use parentheses whenever you want to be explicit or the order isn't obvious. Clear code beats clever code.


Summary

  • Arithmetic+ - * / % ** — do math
  • Assignment= += -= *= etc — store values
  • Comparison=== !== > < >= <= — compare values, return boolean
  • Logical&& || ! — combine or reverse conditions
  • Increment/Decrement++ -- — add or subtract 1
  • Ternarycondition ? a : b — shorthand if/else
  • Always use === over ==
  • Use parentheses when operator order isn't obvious

On this page