Define what missing means
A form value of zero may be valid, a checkbox value of false may be intentional, and an empty string may mean the user cleared a field. Treating every falsy value as missing can erase those choices. Before choosing a defaulting operator, describe which values your contract accepts and which values represent absence. Syntax should follow that decision.
Default only null or undefined when appropriate
The nullish coalescing operator uses the fallback for null or undefined, preserving other values. For a pagination setting, pageSize ?? 20 leaves zero untouched, while pageSize || 20 replaces it. Whether zero should be accepted is a separate validation decision. Keeping defaulting and validation distinct makes a settings screen easier to explain and test.
Read optional branches carefully
Optional chaining can read profile.address?.city without throwing when address is absent. It does not confirm that the remaining value has the correct type or a meaningful value. An API returning city as an object still needs validation. Use the operator for an intentionally optional branch, not as a substitute for understanding the structure of an external response.
Keep boundaries honest
Consider an analytics card where metrics?.activeUsers ?? 0 displays zero. That may be correct for a known empty result, but misleading during a failed request or before data loads. Represent loading, unavailable and empty states separately if they mean different things to the reader. A calm-looking fallback should not conceal that the application does not know the answer.
Avoid accidental grouping and access mistakes
Keep a chain readable instead of mixing several levels of access, calls and fallbacks in one dense expression. An optional method call still requires a callable value when a non-null value exists. Similarly, adding parentheses can end the optional portion of a chain before later access. Extract intermediate values when the behavior is not obvious during review.
Build a small truth table
Verify the expression with undefined, null, zero, false, an empty string and a valid value. Then add malformed external data and a network failure to the screen-level checks. Write down the expected label for each situation. This modest exercise often catches bugs earlier than a test that only supplies the usual happy-path object.
Further reading
Put the idea to work.
Explore free browser tools for images, text and developer workflows.