Operators Overview
Operators are symbols or keywords that perform operations on values —
from simple math like + and *
to powerful tools like typeof and **.
Mastering them is the foundation of every JavaScript expression you'll ever write.
Operator Categories
JavaScript operators fall into several distinct categories. Each category handles a specific type of operation — from crunching numbers to inspecting types. This chapter covers the five most essential groups you'll use in everyday code.
// Arithmetic
5 + 3 // 8
10 % 3 // 1 (remainder)
2 ** 8 // 256 (exponent)
// Assignment
let x = 10;
x += 5; // x is now 15
x **= 2; // x is now 225
// String concatenation
"Hello" + " World"; // "Hello World"
`Score: ${100}`; // "Score: 100"
// Unary
typeof "hi"; // "string"
typeof 42; // "number"===,
!==, <, >)
and logical operators (&&, ||,
??),
which are covered in the next two chapters.
Arithmetic Operators
Arithmetic operators perform standard mathematical calculations.
JavaScript supports all the classic operators plus two powerful extras —
the exponentiation (**) operator
added in ES2016 and the modulo (%) operator
for remainder division.
// Basic math
const a = 10, b = 3;
console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.3333...
console.log(a % b); // 1 (remainder of 10 ÷ 3)
console.log(a ** b); // 1000 (10³)
// Increment / Decrement
let count = 5;
console.log(count++); // 5 (returns THEN increments)
console.log(count); // 6
console.log(++count); // 7 (increments THEN returns)
// Common use-case: check even or odd
const isEven = n => n % 2 === 0;
isEven(4); // true
isEven(7); // false
// Division gotcha
10 / 0; // Infinity (not an error!)
0 / 0; // NaNInfinity (for n / 0) or NaN
(for 0 / 0). Always validate divisors in critical code.
Assignment Operators
The basic assignment operator =
stores a value in a variable. Compound assignment operators combine
an arithmetic (or logical) operation with assignment, making code shorter and more readable.
| Operator | Equivalent To | Name | Example |
|---|---|---|---|
| = | x = value | Simple assignment | let x = 10 |
| += | x = x + n | Add & assign | x += 5 → 15 |
| -= | x = x - n | Subtract & assign | x -= 3 → 12 |
| *= | x = x * n | Multiply & assign | x *= 2 → 24 |
| /= | x = x / n | Divide & assign | x /= 4 → 6 |
| %= | x = x % n | Modulo & assign | x %= 4 → 2 |
| **= | x = x ** n | Exponent & assign | x **= 3 → 8 |
| ??= | x = x ?? val | Nullish assign (ES2021) | x ??= "default" |
| &&= | x = x && val | Logical AND assign | x &&= "updated" |
| ||= | x = x || val | Logical OR assign | x ||= "fallback" |
let score = 100;
score += 20; // 120 (add and assign)
score -= 15; // 105 (subtract and assign)
score *= 2; // 210 (multiply and assign)
score /= 3; // 70 (divide and assign)
score %= 11; // 4 (modulo and assign)
score **= 2; // 16 (exponent and assign)
// ES2021 logical assignment operators
let user = null;
user ??= "Guest"; // "Guest" (assigns if null/undefined)
let name = "Alice";
name ??= "Guest"; // "Alice" (already has a value)
let config = {};
config.debug ||= true; // assigns true if config.debug is falsyString Operators & Concatenation
The + operator doubles as a
string concatenation operator when either operand is a string.
JavaScript will coerce the other operand to a string automatically.
Modern code prefers template literals (`${}`),
but knowing how + behaves is essential for debugging coercion bugs.
// String + string → concatenation
"Hello" + " " + "World"; // "Hello World"
// += for string appending
let msg = "Good";
msg += " morning"; // "Good morning"
// Number coercion — the classic gotcha!
"5" + 3; // "53" ← string wins, NOT 8
"5" - 3; // 2 ← - converts string to number
"5" * 2; // 10 ← * also converts
// Why: + prefers strings; -, *, / prefer numbers
1 + 2 + "3"; // "33" (1+2=3, then 3+"3"="33")
"1" + 2 + 3; // "123" (left-to-right: "1"+2="12", "12"+3="123")
// ✅ Template literals — preferred modern syntax
const firstName = "Alice";
const score = 98;
const greeting = `Hello, ${firstName}! Your score is ${score}.`;
// "Hello, Alice! Your score is 98."
// Multi-line template literal
const html = `
<div class="card">
<h2>${firstName}</h2>
<p>Score: ${score}</p>
</div>
`;"5" + 3 gives "53" (string concatenation),
not 8. The + operator checks operand types
before deciding whether to add or concatenate.
Use Number("5") + 3 or parseInt("5") + 3
when you need arithmetic with string inputs.
Unary Operators
Unary operators take a single operand.
They include type inspection (typeof),
numeric conversion (+value),
property deletion (delete),
and boolean negation (!).
!! converts any value to
its boolean equivalent.undefined. Used
in href="javascript:void(0)".// typeof — inspect any value
typeof 42; // "number"
typeof "hello"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object" ← famous bug, not fixed for legacy reasons
typeof {}; // "object"
typeof []; // "object" ← arrays are objects!
typeof function(){}; // "function"
// Unary + (convert to number)
+"42"; // 42
+""; // 0
+true; // 1
+false; // 0
+null; // 0
+undefined;// NaN
// delete — remove an object property
const user = { name: "Alice", age: 30, role: "admin" };
delete user.role;
console.log(user); // { name: "Alice", age: 30 }
// ! and !! (boolean operations)
!true; // false
!0; // true (0 is falsy)
!!"hello"; // true (convert to boolean)
!!0; // falsetypeof null === "object" is a known bug in JavaScript
that has existed since 1995. It can never be fixed because that would
break billions of websites. To safely check for null, always use
value === null.
Operator Precedence
Operator precedence determines the order in which operators are evaluated when multiple operators appear in a single expression. Higher precedence operators evaluate before lower ones — just like math: multiplication before addition.
| Priority | Operator(s) | Type | Associativity |
|---|---|---|---|
| 19 | ( ) | Grouping (always wins) | n/a |
| 18 | . [ ] ?. new() | Member / optional access, new with args | Left → Right |
| 17 | new (no args) | new without argument list | Right → Left |
| 16 | x++ x-- | Postfix increment / decrement | n/a |
| 15 | ! ~ +x -x ++x --x typeof void delete | Unary operators (prefix) | Right → Left |
| 14 | ** | Exponentiation | Right → Left |
| 13 | * / % | Multiplicative | Left → Right |
| 12 | + - | Additive / Concatenation | Left → Right |
| 11 | < <= > >= in instanceof | Relational | Left → Right |
| 10 | == != === !== | Equality | Left → Right |
| 6 | && | Logical AND | Left → Right |
| 5 | || | Logical OR | Left → Right |
| 4 | ?? | Nullish Coalescing | Left → Right |
| 3 | ? : | Ternary / Conditional | Right → Left |
| 2 | = += -= *= /= %= **= ??= &&= ||= | Assignment | Right → Left |
| 1 | , | Comma (sequence) | Left → Right |
// Multiplication before addition (just like math)
2 + 3 * 4; // 14 (3*4=12 first, then +2)
(2 + 3) * 4; // 20 (brackets override)
// Exponentiation is right-associative
2 ** 3 ** 2; // 512 (2**(3**2) = 2**9 = 512, NOT (2**3)**2 = 64)
// typeof binds tighter than +
typeof 1 + 2; // "number2" (typeof 1 → "number", then +"2" → concat)
typeof (1 + 2); // "number" (brackets first: 1+2=3, typeof 3)
// Logical AND before OR
true || false && false; // true (&& first: false&&false=false; true||false=true)
// Assignment has the lowest precedence
let x = 2 + 3 * 4; // Right side evaluates first → x = 14
// Practical tip: when in doubt, use parentheses
const result = (price * quantity) + (tax * 0.1) - discount;() to make your intent explicit —
it both forces the evaluation order and makes your code dramatically
easier to read and maintain.
Try It — Live Editor
Experiment with JavaScript operators directly below. Change values, test precedence, or explore type coercion — all without leaving this page.
Common Patterns & Recipes
These operator patterns appear constantly in real JavaScript projects. Memorise them and you'll write cleaner, faster, more idiomatic code.
// ── Even / Odd check ─────────────────────────────────────────
const isEven = n => n % 2 === 0;
isEven(8); // true
isEven(7); // false
// ── Swap two variables (destructuring) ───────────────────────
let a = 1, b = 2;
[a, b] = [b, a]; // a=2, b=1
// ── Convert a string to number ───────────────────────────────
+"3.14"; // 3.14 (unary plus)
Number("3.14"); // 3.14 (explicit constructor)
~~3.9; // 3 (double bitwise NOT — fast floor for positives)
// ── Truncate a decimal without Math.floor ────────────────────
const total = 99.9;
const cents = (total * 100) | 0; // 9990 (bitwise OR 0 truncates)
// ── Exponent shorthand ────────────────────────────────────────
const KB = 2 ** 10; // 1024
const MB = 2 ** 20; // 1048576
const GB = 2 ** 30; // 1073741824
// ── Conditional (ternary) with operator precedence ────────────
const age = 20;
const ticket = age >= 18 ? "Full price" : "Child price";
// "Full price"
// ── Safe number check with typeof ─────────────────────────────
function add(a, b) {
if (typeof a !== "number" || typeof b !== "number") {
throw new TypeError("Both arguments must be numbers");
}
return a + b;
}
// ── Clamp a value between min and max ───────────────────────
const clamp = (n, min, max) => Math.min(Math.max(n, min), max);
clamp(150, 0, 100); // 100
"5" + 3 evaluate to in JavaScript?2 + 3 * 4?typeof null return?- JavaScript operators fall into five main categories: arithmetic, assignment, string, unary, and operators with specific precedence rules.
- Arithmetic operators include
+,-,*,/,%(modulo), and**(exponentiation — ES2016). Dividing by zero returnsInfinity, never throws. - Compound assignment operators like
+=,-=,*=are shorthand that combine arithmetic and assignment in one step. - The
+operator concatenates when either operand is a string. Prefer template literals (`${}`) for readable multi-variable strings. typeofreturns a string like"number","string", or"boolean".typeof null === "object"is a legacy bug — usevalue === nullto check for null.- Unary
+valueis the fastest way to convert a string to a number.!!converts any value to its boolean equivalent. - Operator precedence follows PEMDAS-like rules:
()>**>* / %>+ -> comparisons >&&>||>??> assignment. When in doubt, add parentheses. - The ES2021 logical assignment operators
??=,&&=, and||=combine a logical check with assignment in one clean expression.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.