Tutorials JavaScript Operators Overview
Ch 9 / 78
Chapter 13 Beginner 7 min read JavaScript

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.

What you'll learn
All 5 categories of JavaScript operators
Arithmetic operators: +, -, *, /, %, **
Assignment operators and shorthand forms
String concatenation and template literals
Unary operators: typeof, void, delete, !
Operator precedence and evaluation order

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
Math operations: add, subtract, multiply, divide, modulo, exponent
Assignment
Assign or update variable values: =, +=, -=, *=, /=, **=
String
Concatenate strings using + or build with template literals
Unary
Single-operand operators: typeof, void, delete, +, -, !
Precedence
Evaluation order when multiple operators appear in one expression
JavaScript — Quick Operator Taste
// 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"
Note
JavaScript also includes comparison operators (===, !==, <, >) 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.

+
Addition
Adds two numbers. Also used for string concatenation.
5 + 3 → 8
−
Subtraction
Subtracts the right operand from the left.
10 - 4 → 6
*
Multiplication
Multiplies two numbers together.
4 * 3 → 12
/
Division
Divides the left operand by the right. Returns a float if needed.
9 / 4 → 2.25
%
Modulo (Remainder)
Returns the remainder after integer division. Great for even/odd checks.
10 % 3 → 1
**
Exponentiation (ES2016)
Raises the left value to the power of the right. Replaces Math.pow().
2 ** 10 → 1024
++
Increment
Adds 1. Prefix (++x) increments first; postfix (x++) returns then increments.
let x=5; x++ → 5 then 6
−−
Decrement
Subtracts 1. Same prefix/postfix behaviour as increment.
let x=5; --x → 4
JavaScript — Arithmetic in Action
// 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; // NaN

Watch Out
Dividing by zero in JavaScript does not throw an error — it returns Infinity (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"
JavaScript — Assignment Operators
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 falsy

String 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.

JavaScript — String Concatenation vs Template Literals
// 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>
`
;

Coercion Gotcha
"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 (!).

typeof
Type Inspection
Returns a string representing the type of its operand. The most used unary operator in debugging.
typeof 42 → "number"
!
Logical NOT
Inverts a boolean. Double !! converts any value to its boolean equivalent.
!true → false · !!0 → false
+value
Unary Plus (Convert)
Converts operand to a number. The fastest way to cast a string or boolean to number.
+"42" → 42 · +true → 1
-value
Unary Minus (Negate)
Negates a number. Also converts the operand to a number first, like unary plus.
-"5" → -5 · -(-3) → 3
delete
Property Deletion
Removes a property from an object. Returns true if successful, false otherwise.
delete obj.name → true
void
void Operator
Evaluates an expression and returns undefined. Used in href="javascript:void(0)".
void 0 → undefined
JavaScript — Unary Operators
// 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; // false
typeof null Bug
typeof 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
JavaScript — Precedence in Practice
// 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;
Pro Tip
You don't need to memorise the entire precedence table. Use parentheses () 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.

Live Editor
✎ Editor
👁 Preview

Common Patterns & Recipes

These operator patterns appear constantly in real JavaScript projects. Memorise them and you'll write cleaner, faster, more idiomatic code.

JavaScript — Operator Recipes
// ── 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
⚡
Free Resource
Download our JavaScript Cheat Sheet
All operators, precedence table & coercion rules — one printable page.
Get Sheet
Quick Check
What does "5" + 3 evaluate to in JavaScript?
Quick Check
What is the result of 2 + 3 * 4?
Quick Check
What does typeof null return?
Chapter Summary
  • 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 returns Infinity, 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.
  • typeof returns a string like "number", "string", or "boolean". typeof null === "object" is a legacy bug — use value === null to check for null.
  • Unary +value is 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

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel