$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #70

Which fix removes the subtle bug?

const cfg = { retries: 0 };
const retries = cfg.retries || 3;
console.log(retries);
  1. Use ?? so only null/undefined default
  2. Use && so only null/undefined default
  3. Change retries: 0 to retries: null
  4. Compare explicitly with cfg.retries > 0
0 is falsy so || replaces a valid value; ?? only falls back on null or undefined.
Play Problem Solving ▸