JS Operators
In this page:
Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers, including addition, subtraction, multiplication, division, and more specialized operations like remainder and exponentiation.
Note: The + operator also works on strings to join them together, its behavior depends entirely on the types of values involved.
Warning: Dividing by zero in JavaScript does not throw an error, it returns Infinity or -Infinity instead, which can hide bugs if not checked for.
Example: Arithmetic Operators
console.log(10 + 3);
console.log(10 % 3);
console.log(2 ** 3);
Comparison Operators
Comparison operators compare two values and produce a boolean result, true or false, including equal to, not equal to, greater than, and less than, with strict versions that also check type.
Note: Prefer the strict === and !== over == and != in almost all cases, they avoid surprising type conversion bugs.
Warning: == considers 5 (a string) and 5 (a number) equal after converting types, while === correctly treats them as different.
Example: Comparison Operators
console.log(5 == "5");
console.log(5 === "5");
console.log(5 > 3);
Logical Operators
Logical operators combine or invert boolean values, && (and) requires both sides to be true, || (or) requires at least one side to be true, and ! (not) flips a boolean's value entirely.
Note: Logical operators are essential for combining multiple conditions in an if statement without nesting them separately.
Warning: && and || don't always return strict true or false, they return one of the original operand values, which matters in more advanced usage.
Example: Logical Operators
console.log(true && false);
console.log(true || false);
console.log(!true);
Assignment and Ternary Operators
The assignment operator = stores a value in a variable, and JavaScript also offers a compact ternary operator, condition ? valueIfTrue : valueIfFalse, as a shorthand for a simple if/else that produces a value.
Note: Reach for the ternary operator when you need to pick between two simple values, but keep using full if/else for more complex logic.
Warning: Nesting multiple ternary operators together can quickly become difficult to read, prefer if/else once logic gets more complex.
Example: Assignment and Ternary Operators
let age = 20;
let status = age >= 18 ? "adult" : "minor";
console.log(status);
typeof and Spread Operators
typeof returns a string describing a value's type, useful for checking what kind of data you're working with, while the spread operator ... expands an array or object's elements individually, commonly used for copying or combining collections.
Note: typeof is especially handy for quickly checking whether a value is a number, string, or something unexpected before processing it further.
Warning: typeof null famously returns object, a long-standing quirk in JavaScript that catches many developers off guard.
Example: typeof and Spread Operators
console.log(typeof "hello");
console.log(typeof null);
const nums = [1, 2, 3];
console.log([...nums, 4, 5]);
- Confusing = (assignment) with == or === (comparison), a single equals sign always assigns, never compares.
- Using == instead of === for comparisons, which can produce unexpected results due to automatic type conversion.
- Forgetting operator precedence, like assuming addition always happens before multiplication in a mixed expression.
- Arithmetic operators perform math, comparison operators test relationships, and logical operators combine boolean results.
- The assignment operator = sets a value, while === strictly compares both value and type without conversion.
- The ternary operator offers a compact one-line alternative to a simple if/else statement.
All core JavaScript operators, arithmetic, comparison, logical, assignment, ternary, typeof, and spread, are supported identically across every modern browser.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: