Use concise null checking patterns to prevent runtime errors and improve code readability when handling potentially undefined values.
When checking if a value is neither null nor undefined:
// Preferred: Concise null check
if (err != null) {
// err is neither null nor undefined
}
// Instead of verbose explicit checks
if (err !== undefined && err !== null) {
// Same result but more typing
}
Before accessing properties of objects that might be null/undefined:
// Check before access to prevent "cannot read property of undefined" errors
if (reply.request.socket != null && !reply.request.socket.destroyed) {
// Safe to use socket property
}
For default values, leverage nullish coalescing:
// Assign default value if property is null or undefined
reply[kReplyHeaders]['content-type'] = reply[kReplyHeaders]['content-type'] ?? 'application/json; charset=utf-8'
// Or with fallback chains when appropriate
this[kRequestOriginalUrl] = this.raw.originalUrl || this.raw.url
These patterns ensure your code handles null and undefined values safely while remaining concise and readable.
Enter the URL of a public GitHub repository