JavaScript

Lesson 01

Strict equality and nullish checks

Compare values intentionally and preserve meaningful falsy values.

Good Code

review-options.js
function formatReviewState(state) {
  // Strict equality avoids coercion hiding unexpected values.
  if (state === "approved") {
    return "Approved";
  }

  if (state === "changes_requested") {
    return "Changes requested";
  }

  return "Pending";
}

function getRetryCount(options) {
  return options.retryCount ?? 3;
}

Bad Code

review-options.js
function formatReviewState(state) {
  // Loose equality and || blur missing data with valid falsy values.
  if (state == "approved") {
    return "Approved";
  }

  if (state == "changes_requested") {
    return "Changes requested";
  }

  return "Pending";
}

function getRetryCount(options) {
  return options.retryCount || 3;
}

Review Notes

What to review

Good Code

The good version compares exact values and only falls back when retryCount is null or undefined, so 0 remains a valid explicit value.

Bad Code

The bad version depends on coercion and uses ||, which treats valid falsy values like 0 as missing configuration.

Takeaways

  • Use strict equality for comparisons and nullish coalescing when only null or undefined should fall back.