JS Operators
JavaScript Operators: Arithmetic, Comparison, Logical, and Nullish
JavaScript operators perform math, assign values, compare data, and combine boolean conditions. Knowing how operators interact and which ones run first helps you write bug-free expressions across real applications.
Essential Operator Groups
JavaScript groups operators by the kind of work they perform on values (called operands):
| Category | Common Operators | Purpose |
|---|---|---|
| Arithmetic | +, -, *, /, %, ** |
Perform math and remainders |
| Comparison | ===, !==, >, <, >=, <= |
Return true or false |
| Logical | &&, ` |
|
| Ternary | condition ? a : b |
Inline conditional expression |
Logical operators short-circuit from left to right. When the left side of && is falsy, JavaScript skips evaluating the right side completely. The same shortcut applies when the left side of || is truthy.
Modern Fallback Operators: ?? vs ||
Use the nullish coalescing operator (??) when you want a fallback only for null or undefined. Unlike ||, ?? keeps valid falsy values such as 0, "", or false.
const userPort = 0;
const safePort = userPort ?? 8080; // Keeps 0
const wrongPort = userPort || 8080; // Falls back to 8080!
const isLoggedIn = true;
const isAdmin = false;
const badge = isLoggedIn && isAdmin ? "Admin Panel" : "Member View";
console.log({ safePort, wrongPort, badge });
Operator Precedence Basics
JavaScript evaluates * and / before + and -, and evaluates && before ||. Whenever an expression mixes multiple operators, wrap parts in parentheses (...) so intent stays crystal clear to every reader.
Common Mistakes to Avoid
- Using
==instead of===: Loose equality converts types behind the scenes ("5" == 5istrue), causing subtle bugs. Always use strict===and!==. - Mixing up
||and??: Usingcount || 10replaces0with10. Usecount ?? 10for numeric defaults. - Over-nesting ternary operators: Chaining three
? :operators on one line is hard to read; switch toif...elseblocks instead.
Frequently Asked Questions
What is the difference between && and || in JavaScript?
&& returns the first falsy operand or the last operand if all are truthy. || returns the first truthy operand or the last operand if all are falsy.
What does the typeof operator return?
typeof is a unary operator that returns a lowercase string naming the operand's type, such as "number", "string", or "boolean".
What does the ternary operator do?
The ternary operator cond ? valA : valB evaluates cond and returns valA when truthy or valB when falsy.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.