Back to all reviewers

Use modern nullish operators

nodejs/node
Based on 3 comments
JavaScript

When dealing with potentially null or undefined values, use optional chaining (`?.`) and nullish coalescing (`??`) operators instead of verbose conditionals. These operators make code more readable, concise, and prevent null reference errors.

Null Handling JavaScript

Reviewer Prompt

When dealing with potentially null or undefined values, use optional chaining (?.) and nullish coalescing (??) operators instead of verbose conditionals. These operators make code more readable, concise, and prevent null reference errors.

// Verbose and error-prone
if (object && object.property) {
  value = object.property;
} else {
  value = defaultValue;
}

// Concise and safe
value = object?.property ?? defaultValue;

In real-world code, these operators simplify common patterns:

// From discussion #1: Safely access property with fallback
innerOk(this?.ok ?? ok, args.length, ...args);

// From discussion #13: Conditional assignment with fallback
this.secureOptions = secureOptions || undefined;

// From discussion #15: Lazy loading with nullish assignment
inspect ??= require('util').inspect;
return inspect;

These operators are particularly valuable for:

  • Safely accessing deeply nested properties without verbose checks
  • Setting default values when null or undefined is encountered
  • Simplifying conditional logic around nullable values
  • Reducing boilerplate code for null safety

Remember that optional chaining short-circuits when a reference is nullish, while nullish coalescing only falls back when the left side is specifically null or undefined (not other falsy values like 0 or '').

3
Comments Analyzed
JavaScript
Primary Language
Null Handling
Category

Source Discussions