Use nullish coalescing operators (`??`) and logical OR with safe defaults (`|| {}`) to handle null and undefined values more safely and concisely. This prevents runtime errors when accessing properties on potentially null/undefined objects and provides cleaner, more readable code.
Use nullish coalescing operators (??
) and logical OR with safe defaults (|| {}
) to handle null and undefined values more safely and concisely. This prevents runtime errors when accessing properties on potentially null/undefined objects and provides cleaner, more readable code.
Instead of explicit null/undefined checks:
// Avoid
return this._eventsCount > 0 && this._events !== undefined
? Reflect.ownKeys(this._events)
: [];
// Prefer
return this._eventsCount > 0 ? Reflect.ownKeys(this._events || {}) : [];
For numeric values, use nullish coalescing to distinguish between falsy values and null/undefined:
// Avoid - treats 0 as falsy
this.errno = options.errno || 1;
// Prefer - only coalesces null/undefined
this.errno = options.errno ?? 1;
This approach reduces boilerplate code while providing better null safety and more predictable behavior when dealing with optional properties and parameters.
Enter the URL of a public GitHub repository