JavaScript Operators: Ternary, Optional Chaining, Nullish Coalescing

🚀 JavaScript has three interesting operators that look very similar symbolically, but solve different problems. 1. Ternary (?:) 2. Optional Chaining (?.) 3. Nullish Coalescing (??) ✨ All of them revolve around the question mark (?), yet each has its own pattern! 🔹 Ternary (?:) – Quick condition. const status = age >= 18 ? "Adult" : "Minor"; 🧩 Pattern: condition ? trueValue : falseValue → A compact alternative to if...else. 🔹 Optional Chaining (?.) – Safe property access const city = user?.address?.city; 🧩 Pattern: object?.property → Prevents errors when a property might be null or undefined. 🔹 Nullish Coalescing (??) – Smart fallback const username = inputName ?? "Guest"; 🧩 Pattern: value ?? defaultValue → Uses the default only if the value is null or undefined. 🛠 Simple way to remember: ✔ ?: → Choose between two values. ✔ ?. → Safely access a property. ✔ ?? → Provide a default value. 💡 Three similar symbols, three powerful tools for writing cleaner and safer JavaScript. #JavaScript #WebDevelopment #CleanCode #Frontend

  • graphical user interface, application
See more comments

To view or add a comment, sign in

Explore content categories