Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I safely read a nested property that may be missing?

Quick answer

Use ?. at each point where the value to its left might be null or undefined. For example, user?.profile?.name evaluates to undefined if user or profile is nullish, rather than trying to read the remaining property. Ordinary chained access can fail at a nullish link. Optional chaining gives you a result; it does not decide what your application should do when the value is missing. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have an object that may not include every nested field. What goes wrong with ordinary property access?

Geez

Suppose a record is expected to have a profile object and a name property:

const user = { profile: { name: "Mina" } };

With this value, user.profile.name reads the name. But if user is null or undefined, trying to read profile fails. If user exists but user.profile is null or undefined, trying to read name fails instead. The problem is continuing the access through a value that is nullish.

The useful first step is to identify which links in the chain might be absent. Optional chaining can prevent an access error at those points, but it does not guarantee that every value in your program is valid. [S1]

Follow-up question

How do I write the nested read, and what value do I get if an intermediate link is absent?

Geez

Use ?. where the value on its left may be nullish:

const name = user?.profile?.name;

If user is null or undefined, the chain short-circuits and name becomes undefined. If user exists but profile is null or undefined, the result is also undefined. If both objects exist and name is present, the expression reads that value. At a nullish point, the operator prevents the remaining access instead of attempting it. [S1]

For comparison, ordinary user.profile.name can fail if user or user.profile is nullish. Optional chaining prevents that interrupted access; it does not decide what a missing name should mean in your application.

Follow-up question

What if I want a label to display when the name is missing?

Geez

Choose that behavior explicitly. You could assign a fallback when the chain produces undefined, or handle the missing name in a separate branch. For example:

const name = user?.profile?.name;

Then decide what to do if name is undefined: display a suitable label, omit the label, or handle the missing data another way. Those choices depend on the purpose of the field. Optional chaining makes the read safe at a nullish link; it does not supply a fallback or choose one for you. [S1]

For instance, with const user = { profile: null };, user?.profile?.name evaluates to undefined. Your code still needs a deliberate response to that result.

Follow-up question

Can you give me a quick pattern to apply to another nested object?

Geez

Identify the earliest link that might be nullish, then use ?. at each potentially absent link before reading farther. For example, if an order or its shipping address may be absent:

const city = order?.shippingAddress?.city;

This gives you undefined if a nullish value interrupts the chain. Then decide what that result means in this part of your code. You might show a fallback, omit the output, or handle the missing data separately. If the value is required for an operation, make that requirement clear rather than relying on optional chaining to decide what should happen.

A useful review question is: “If this link is absent, should I show a fallback, omit this output, or handle the missing data another way?” Optional chaining handles the interrupted read; your code still needs an intentional answer. [S1]

A practical checklist

  • Mark which objects or nested links may be null or undefined.

  • Add ?. at each possibly nullish link before reading farther.

  • Account for the undefined result when a nullish value interrupts the chain.

  • Choose an explicit response that fits the application.

  • Check the expression with a complete object and one with a missing intermediate link.

A few more useful answers

Does optional chaining replace a missing value with a default?

No. If a nullish value interrupts the chain, the expression evaluates to undefined. Decide separately whether to use a fallback or handle the missing value another way. [S1]

Should I use `?.` on every property access?

Use it where a value to the left may be null or undefined and you want to avoid attempting the remaining access. It does not validate required data or decide what missing information should mean. [S1]

Sources

References used for this guide.

  1. [S1] Optional chaining (?.) — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work