Back to all reviewers

use loose equality checks

serverless/serverless
Based on 6 comments
JavaScript

Use loose equality (`== null`) instead of strict equality (`=== null` or `=== undefined`) when checking for both null and undefined values. In most JavaScript contexts, null and undefined should be treated equivalently, and loose equality provides a more concise and readable way to handle both cases.

Null Handling JavaScript

Reviewer Prompt

Use loose equality (== null) instead of strict equality (=== null or === undefined) when checking for both null and undefined values. In most JavaScript contexts, null and undefined should be treated equivalently, and loose equality provides a more concise and readable way to handle both cases.

This pattern is particularly useful for:

  • Environment variable checks
  • Optional configuration properties
  • Function parameters with default values
  • Property existence validation

Example:

// Instead of this:
if (typeof process.env[address] === 'undefined') missingEnvVariables.add(address);
if (startingPosition === 'AT_TIMESTAMP' && !(startingPositionTimestamp !== undefined && startingPositionTimestamp !== null)) {

// Use this:
if (process.env[address] == null) missingEnvVariables.add(address);
if (startingPosition === 'AT_TIMESTAMP' && startingPositionTimestamp == null) {

The loose equality check (== null) catches both null and undefined values in a single, readable condition, making code more maintainable and following JavaScript’s conventional approach to null safety.

6
Comments Analyzed
JavaScript
Primary Language
Null Handling
Category

Source Discussions