JS Operators
In this page:
result = operand1 operator operand2;
Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers, including addition, subtraction, multiplication, division, and more specialized operations like remainder and exponentiation.
Example: Arithmetic Operators
// Print `10 + 3` to the console
// Print `10 + 3` to the console
console.log(10 + 3);
// Print `10 % 3` to the console
// Print `10 % 3` to the console
console.log(10 % 3);
// Print `2 ** 3` to the console
// Print `2 ** 3` to the console
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.
Example: Comparison Operators
// Print `5 == "5"` to the console
// Print `5 == "5"` to the console
console.log(5 == "5");
// Print `5 === "5"` to the console
// Print `5 === "5"` to the console
console.log(5 === "5");
// Print `5 > 3` to the console
// Print `5 > 3` to the console
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.
Example: Logical Operators
// Print `true && false` to the console
// Print `true && false` to the console
console.log(true && false);
// Print `true || false` to the console
// Print `true || false` to the console
console.log(true || false);
// Print `!true` to the console
// Print `!true` to the console
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.
Example: Assignment and Ternary Operators
// Declare the variable `age`, set to `20`
// Declare the variable `age`, set to `20`
let age = 20;
// Declare the variable `status`, set to `age >= 18 ? "adult" : "minor"`
// Declare the variable `status`, set to `age >= 18 ? "adult" : "minor"`
let status = age >= 18 ? "adult" : "minor";
// Print `status` to the console
// Print `status` to the console
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.
Example: typeof and Spread Operators
// Print `typeof "hello"` to the console
// Print `typeof "hello"` to the console
console.log(typeof "hello");
// Print `typeof null` to the console
// Print `typeof null` to the console
console.log(typeof null);
// Declare the constant `nums`, set to `[1, 2, 3]`
// Declare the constant `nums`, set to `[1, 2, 3]`
const nums = [1, 2, 3];
// Print `[...nums, 4, 5]` to the console
// Print `[...nums, 4, 5]` to the console
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: