Curriculum
Concept 18 // Module 02: Values, Operators & Control Flow
Operators, Precedence & Associativity
Module 02: Values, Operators & Control Flow•Concept 18 of 100• 5 min read
Operators, Precedence & Associativity
Operators are symbols that perform operations on values (operands). JavaScript provides arithmetic, logical, comparison, and several modern convenience operators like optional chaining and nullish coalescing.
The Core Concept
Syntax Reference
// 1. Arithmetic
1 + 2; 5 - 3; 10 * 2; 10 / 2; 10 % 3; 2 ** 3;
// 2. Comparison
5 > 3; 5 === 5; 5 !== 3;
// 3. Logical
true && false; true || false; !true;
// 4. Modern JS Operators
const value = a ?? "fallback"; // Nullish Coalescing
const name = user?.profile?.id; // Optional Chaining
Live Interactive Studio
The `||` operator returns the right side if the left is *any* falsy value (0, '', false). The `??` operator only returns the right side if the left is `null` or `undefined`.
JavaScript Editor
Loading...
Console Output
Waiting for output...
Key Code Patterns in Action
01
Nullish Coalescing (??) vs OR (||)
// 1. Nullish Coalescing (??) vs OR (||)
let count = 0;
let result1 = count || 10; // OR sees 0 as falsy
console.log(result1); // 10 (Maybe not what you wanted!)
let result2 = count ?? 10; // Nullish only looks for null/undefined
console.log(result2); // 0 (Correct!)The `||` operator returns the right side if the left is *any* falsy value (0, '', false). The `??` operator only returns the right side if the left is `null` or `undefined`.
02
Optional Chaining (?.)
// 2. Optional Chaining (?.)
const user = {
id: 1,
// profile is missing
};
// console.log(user.profile.name); // ❌ Throws Error
console.log(user.profile?.name); // ✅ undefined (Safe!)Optional chaining `?.` stops evaluation if the value before it is `null` or `undefined`, preventing the dreaded 'Cannot read property of undefined' error.
03
Strict Equality (===) vs Loose (==)
// 3. Strict Equality (===) vs Loose (==)
console.log(5 == "5"); // true (Coercion happens)
console.log(5 === "5"); // false (Type must match)
console.log(null == undefined); // true
console.log(null === undefined); // falseAlways use `===` (strict equality) to avoid unexpected type coercion bugs.
04
1. Core Usage: Operators, Precedence & Associativity
// Core syntax and usage for Operators, Precedence & Associativity
const feature = 'operators-precedence-associativity';
console.log('Concept 18:', feature);
console.log('Active:', true);Illustrates the fundamental invocation and syntax structure for Operators, Precedence & Associativity.
05
2. Defensive Handling & Edge Case
// Edge-case defensive handling
function handleSafe(input) {
if (input === null || input === undefined) return 'safe-fallback';
return String(input).trim();
}
console.log(handleSafe('operators-precedence-associativity'));
console.log(handleSafe(null));Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with Operators, Precedence & Associativity.
06
3. Clean Architecture & Production Pattern
// Production-grade pattern for Operators, Precedence & Associativity
const settings = Object.freeze({
id: 'operators-precedence-associativity',
ready: true,
timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);Demonstrates clean, immutable production code avoiding unintended state mutations.
Real-World Engineering Takeaways
Arithmetic for calculations, increments, and string concatenation.
Optional Chaining when dealing with API responses where data might be nested or missing.
Nullish Coalescing for setting default values when `0` or `false` are valid inputs.
Ternary Operator (`? :`) for quick conditional assignments in JSX or templates.
Under the Hood: Engine & Memory Mechanics
How JavaScript Executes This
### **How Operators Work**
**1. Evaluation:**
- JS Engine evaluates expressions from left to right, following **operator precedence** (PEMDAS).
- Intermediary values are stored in **temporary registers** in the CPU or on the **Stack**.
**2. Short-circuiting:**
- For `&&` and `||`, the engine stops as soon as the result is certain.
- If `a` is false in `a && b`, `b` is never even looked at in memory.
**3. Type Coercion:**
- If types don't match, JS will often convert them automatically (Coercion) in the **Stack** before performing the operation.
Quick Knowledge Check
Test your understanding on these 10 questions (one by one):
Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model
What is the core purpose of Operators, Precedence & Associativity in JavaScript?
// Concept 18: Operators, Precedence & Associativity
// Understanding the core mental model1 / 10