Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
Why does optional chaining work with nullish coalescing in my browser, but not in Node.js?
Asked on Dec 31, 2025
Answer
Optional chaining and nullish coalescing are modern JavaScript features introduced in ES2020. They allow for safer property access and handling of nullish values. However, these features may not be available in all environments, especially older versions of Node.js.
const obj = { a: { b: null } };
// Optional chaining with nullish coalescing
const value = obj.a?.b ?? 'default value';
console.log(value); // Outputs: 'default value'Additional Comment:
✅ Answered with JavaScript best practices.- Optional chaining (?.) allows you to safely access deeply nested properties without having to check each level.
- Nullish coalescing (??) provides a default value when the left-hand side is null or undefined.
- These features are supported in modern browsers and Node.js 14 and above. If you're using an older version of Node.js, consider upgrading or using a transpiler like Babel.
- Always check the compatibility of new JavaScript features with your target environment.
Recommended Links:
