Logical, Ternary and Nullish Operators
&& returns the first falsy value or the last value, and || returns the first truthy value. ?? (nullish coalescing) gives a default only for null or undefined, so 0 and an empty string are kept. The ternary operator condition ? a : b is a short if/else.
Optional chaining user?.address?.city returns undefined instead of throwing when something in the middle is missing.
Example
const count = 0;
console.log(count || 10);
console.log(count ?? 10);
const age = 20;
console.log(age >= 18 ? "adult" : "minor");
const user = { name: "Ada" };
console.log(user.address?.city);
console.log(user.address?.city ?? "unknown");Exercise
Use a default only when the value is null or undefined.
const n = value 5;
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.