Skip to content
Justice Tech School

Optional Chaining and Defaults

?. stops and returns undefined if a value is null or undefined. Combine with ?? for defaults. It avoids long guard checks.

Example
const user = { profile: { name: "Ada" } };
console.log(user.profile?.name, user.settings?.theme ?? "light");
console.log(user.getAge?.());

Exercise

Safe property access.

user.name

Quiz Test

What does a?.b give if a is null?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.