Operators
You use operators constantly in JavaScript — every calculation, every comparison, every condition relies on them.
Arithmetic Operators
These perform math on numbers:
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.33... — division
console.log(a % b); // 1 — remainder (modulo)
console.log(a ** b); // 1000 — exponent (10³)
The modulo operator (%) gives the remainder after division. It's very useful for checking if a number is even or odd:
console.log(10 % 2); // 0 — even
console.log(7 % 2); // 1 — odd
Increment and Decrement
These shortcuts add or subtract 1 from a variable:
let count = 0;
count++; // count is now 1
count++; // count is now 2
count--; // count is now 1
// Same as:
count = count + 1;
Assignment Operators
Assignment operators update a variable's value in one step:
let score = 10;
score += 5; // score = score + 5 → 15
score -= 3; // score = score - 3 → 12
score *= 2; // score = score * 2 → 24
score /= 4; // score = score / 4 → 6
score **= 2; // score = score ** 2 → 36
Comparison Operators
Comparison operators compare two values and return true or false:
console.log(5 > 3); // true
console.log(5 < 3); // false
console.log(5 >= 5); // true
console.log(5 <= 4); // false
console.log(5 === 5); // true — strict equal (checks value AND type)
console.log(5 !== 3); // true — strict not equal
Always use === (triple equals) for comparisons in JavaScript, not ==. Triple equals checks both value and type, preventing subtle bugs. For example: "5" == 5 is true, but "5" === 5 is false.
Logical Operators
Logical operators combine multiple conditions:
const age = 20;
const hasTicket = true;
// && (AND) — both must be true
console.log(age >= 18 && hasTicket); // true
// || (OR) — at least one must be true
console.log(age < 18 || hasTicket); // true
// ! (NOT) — flips true to false, and vice versa
console.log(!hasTicket); // false
A practical example — checking if someone can enter:
const age = 20;
const hasID = true;
if (age >= 18 && hasID) {
console.log("Welcome in!");
} else {
console.log("Sorry, you can't enter.");
}
String Concatenation
The + operator joins strings together:
const first = "Coding";
const second = "Banana";
console.log(first + second); // "CodingBanana"
console.log(first + " " + second); // "Coding Banana"
But mixing types can cause surprises:
console.log("5" + 3); // "53" — number is converted to string
console.log("5" - 3); // 2 — string is converted to number
console.log(true + 1); // 2 — true is treated as 1
Try It Yourself
Result will appear here
function calculate() { const a = 15; const b = 4; const sum = a + b; const remainder = a % b; const isEven = a % 2 === 0; document.getElementById('result').textContent = a + ' + ' + b + ' = ' + sum + ' | Remainder: ' + remainder + ' | Is even: ' + isEven; }Have anything to say about this lesson?
Your feedback helps improve these tutorials. If something was confusing or missing, let us know.