JavaScript Notes
Master all JavaScript operators: arithmetic, assignment, comparison, logical, bitwise, ternary, nullish coalescing, and spread. Beginner guide with examples & interview Q&A.
An operator is a symbol that tells JavaScript to perform a specific operation on values (called operands). Operators are literally everywhere in JavaScript code — you use them in every conditional, every calculation, every assignment. This guide covers all operator types with clear examples.
Real World Analogy
Operators are like verbs in mathematics and logic:
- The
+operator is "add" - The
>operator is "is greater than" - The
&&operator is "and" (both must be true) - The
=operator is "assign the value of"
Just like you wouldn't confuse the math symbols = (equals) and ≠ (not equals), you need to know what each JavaScript operator symbol does.
1. Arithmetic Operators
Used to perform mathematical calculations.
let 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 (10 divided by 3 leaves remainder 1)
console.log(a ** b); // 1000 (10 to the power of 3)13 7 30 3.3333333333333335 1 1000
Pre-increment vs Post-increment
5 6 6 6
2. Assignment Operators
Used to assign values to variables.
let score = 100; // = basic assignment
score += 10; console.log(score); // 110 (score = score + 10)
score -= 5; console.log(score); // 105 (score = score - 5)
score *= 2; console.log(score); // 210 (score = score * 2)
score /= 3; console.log(score); // 70 (score = score / 3)
score %= 20; console.log(score); // 10 (score = score % 20)
score **= 2; console.log(score); // 100 (score = score ** 2)110 105 210 70 10 100
Logical Assignment Operators (ES2021)
Default Hello
3. Comparison Operators
Used to compare two values. Always return a boolean (true or false).
console.log(5 == "5"); // true (string coerced to number)
console.log(5 === "5"); // false (different types)
console.log(5 != "5"); // false (loosely equal)
console.log(5 !== "5"); // true (strictly not equal)
console.log(10 > 5); // true
console.log(3 >= 3); // truetrue false false true true true
4. Logical Operators
Used to combine or negate boolean expressions.
&& — AND (both must be truthy)
const age = 20;
const hasID = true;
if (age >= 18 && hasID) {
console.log("Entry allowed");
} else {
console.log("Entry denied");
}Entry allowed
|| — OR (at least one must be truthy)
Access to dashboard
! — NOT (negates the value)
const isLoggedIn = false;
console.log(!isLoggedIn); // true
console.log(!!isLoggedIn); // false (double negation → coerce to boolean)true false
Short-Circuit Evaluation
null Guest
5. Ternary Operator ? :
The only ternary (3-operand) operator. A compact if-else on one line.
Syntax:
condition ? valueIfTrue : valueIfFalseconst marks = 78;
const grade = marks >= 60 ? "Pass" : "Fail";
console.log(grade);
const hour = 14;
const greeting = hour < 12 ? "Good morning" : hour < 18 ? "Good afternoon" : "Good evening";
console.log(greeting);Pass Good afternoon
6. Nullish Coalescing ??
Returns the right operand only if the left is null or undefined. Unlike ||, it doesn't treat 0, "", or false as empty.
No score 0 Anonymous Guest
7. Optional Chaining ?.
Safely accesses nested properties without crashing if any part is null or undefined.
const user = { profile: { city: "Pune" } };
const guestUser = null;
console.log(user?.profile?.city); // "Pune"
console.log(guestUser?.profile?.city); // undefined (no crash!)
console.log(user?.email?.toUpperCase()); // undefined (email doesn't exist)Pune undefined undefined
8. typeof Operator
Returns a string describing the type of a value.
console.log(typeof "hello"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" ← famous bug!
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"
console.log(typeof Symbol()); // "symbol"
console.log(typeof 42n); // "bigint"string number boolean undefined object object object function symbol bigint
9. Spread ... and Rest ... Operators
Same symbol (...), different roles depending on context.
[1, 2, 3, 4, 5] 3 15
Operator Precedence (Order of Operations)
14 20 true false
Common Mistakes
❌ Mistake 1: Using == instead of ===
// ❌ WRONG — loose equality has surprise coercions
console.log(0 == false); // true!
console.log("" == false); // true!
console.log(null == 0); // false
console.log(null == undefined); // true// ✅ CORRECT — always use === unless you specifically need coercion
console.log(0 === false); // false (different types)
console.log("" === false); // false (different types)❌ Mistake 2: Confusing =, ==, and ===
let x = 10;
// ❌ Assignment inside condition — likely a bug!
if (x = 5) { // assigns 5 to x, then checks if 5 is truthy
console.log("This runs!"); // yes it runs, but x is now 5 — bug!
}
// ✅ Comparison
if (x === 5) { // strictly compares x to 5
console.log("x is exactly 5");
}❌ Mistake 3: Not understanding || vs ?? for default values
Comparison: || vs ??
| Expression | `\ | \ | ` result | ?? result |
|---|---|---|---|---|
| `0 \ | \ | "default"` | "default" | 0 |
| `"" \ | \ | "default"` | "default" | "" |
| `false \ | \ | "default"` | "default" | false |
| `null \ | \ | "default"` | "default" | "default" |
| `undefined \ | \ | "default"` | "default" | "default" |
Key Takeaways
- Arithmetic operators (
+,-,*,/,%,**) perform math ===and!==are strict — always prefer them over==and!=- Logical operators (
&&,||,!) use short-circuit evaluation - Ternary
condition ? a : bis a concise one-line if-else ??(nullish coalescing) only falls back fornull/undefined, not other falsy values?.(optional chaining) safely navigates nested properties without crashing- Operator precedence determines evaluation order — use parentheses to be explicit
typeofreturns a string type name;nullbizarrely returns"object"
Interview Questions & Answers
Q1. What is the difference between == and === in JavaScript?
Answer: == is loose equality — it performs type coercion before comparing (converts both sides to a common type). === is strict equality — it compares both value AND type without coercion. Always use === in production code to avoid surprising bugs like 0 == false being true.
Q2. What is the ternary operator and when should you use it?
Answer: The ternary operator (condition ? valueIfTrue : valueIfFalse) is a compact, expression-based alternative to if-else. Use it for simple, readable single-value decisions. Avoid nesting multiple ternaries (readability suffers). For complex conditions, use a regular if-else block.
Q3. Explain short-circuit evaluation in JavaScript.
Answer: Logical operators don't always evaluate both operands:
a && b: ifais falsy,bis never evaluated (returnsa)a || b: ifais truthy,bis never evaluated (returnsa)
This is used practically to write user && user.name (safely access property only if user exists).
Q4. What is the difference between || and ???
Answer: Both provide fallback values, but they trigger differently:
||falls back when the left side is any falsy value (0,"",false,null,undefined,NaN)??only falls back when the left side is null or undefined specifically
Use ?? when 0 or empty string are valid values you want to preserve.
Q5. What does the spread operator ... do?
Answer: Spread expands an iterable (array, object) into individual elements. In array literals: [...arr1, ...arr2] merges arrays. In function calls: Math.max(...nums) passes each element as a separate argument. In object literals: {...obj1, ...obj2} merges objects (shallow copy).
Q6. What is operator precedence and how does it affect results?
Answer: Operator precedence determines the order in which operators are evaluated in an expression. Higher-precedence operators evaluate first (like * before +). 2 + 3 * 4 evaluates as 2 + 12 = 14, not 5 * 4 = 20. Use parentheses to override precedence and make intent explicit.
Q7. What does typeof return for different types?
Answer: typeof returns: "string", "number", "boolean", "undefined", "symbol", "bigint", "function", or "object". Notably, typeof null === "object" (a bug) and typeof [] === "object" (arrays are objects). Use Array.isArray() to detect arrays and explicit === null to check for null.
Q8. What is optional chaining ?. and when should you use it?
Answer: Optional chaining (?.) safely accesses a property or method on an object that might be null or undefined. Instead of throwing a TypeError, it returns undefined. Use it when accessing nested object properties where intermediate values might be absent: user?.address?.city instead of user && user.address && user.address.city.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Operators Complete Guide – Arithmetic, Comparison, Logical & More with Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, basics, operators, javascript operators complete guide – arithmetic, comparison, logical & more with examples
Related JavaScript Master Course Topics