JavaScript Operators: The Basics You Need to Know
Also onpreet-jain.hashnode.dev/javascript-operators-the-basics-you-need-to-knowOperators are the symbols that make things happen. Addition, comparison, logic — they power every piece of JavaScript you'll write. Here's what you need to know.
Arithmetic Operators
These do math. You've seen them before:
const a = 10;
const 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.333... — division
console.log(a % b); // 1 — modulus (remainder)The modulus operator % returns what's left over after division. 10 % 3 is 1 because 3 goes into 10 three times with 1 remaining. This is useful for checking if a number is even or odd:
console.log(10 % 2 === 0); // true — 10 is even
console.log(7 % 2 === 0); // false — 7 is oddComparison Operators
Comparisons return true or false:
console.log(5 > 3); // true — greater than
console.log(5 < 3); // false — less than
console.log(5 >= 5); // true — greater than or equal
console.log(5 <= 4); // false — less than or equalNow, here's the one that trips up beginners — the difference between == and ===.
console.log(5 == '5'); // true — values match after conversion
console.log(5 === '5'); // false — different types, strict comparison== converts types before comparing. '5' becomes 5, so they match. === checks both value and type. Since '5' is a string and 5 is a number, they're not equal.
The safe rule: always use === unless you have a specific reason not to. Unexpected type coercion causes bugs that are hard to find.
The same pattern applies to != vs !==:
console.log(5 != '5'); // false — they coerce to same value
console.log(5 !== '5'); // true — different types, so not equalLogical Operators
Logic operators let you combine conditions:
console.log(true && true); // true — AND: both must be true
console.log(true && false); // false
console.log(true || false); // true — OR: at least one must be true
console.log(false || false); // false
console.log(!true); // false — NOT: flips the value
console.log(!false); // trueA common pattern — checking if a value is in range:
const age = 25;
if (age >= 18 && age < 65) {
console.log('Working age');
}Another — using OR for defaults:
const username = name || 'Anonymous';If name is falsy (empty string, null, undefined), it falls back to 'Anonymous'.
Assignment Operators
The basic assignment operator = assigns a value:
let x = 10;Compound assignment operators combine assignment with an operation:
let x = 10;
x += 5; // x = 15 — add and assign
x -= 3; // x = 12 — subtract and assign
x *= 2; // x = 24 — multiply and assign
x /= 4; // x = 6 — divide and assign
x %= 4; // x = 2 — modulo and assignThese are shorthand for writing x = x + 5. Readable and common.
Practice Assignment
Try these exercises:
- Perform arithmetic operations on two numbers:
const a = 15;
const b = 4;
console.log(a + b); // 19
console.log(a % b); // 3- Compare using both
==and===:
console.log(10 == '10'); // true
console.log(10 === '10'); // false- Write a condition using logical operators:
const temperature = 22;
const isRaining = false;
if (temperature > 20 && !isRaining) {
console.log('Good weather for a walk');
}Wrapping Up
Operators are everywhere in JavaScript. Arithmetic for math, comparisons for conditions, logical operators for combining checks, and assignment for storing values. The syntax is straightforward — the key is knowing the difference between == and === and when to use compound assignment operators. Get these fundamentals solid and the rest of JavaScript becomes much easier to follow.