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

Which fix removes the bug?

function keysOf(v) {
  if (typeof v === 'object') return Object.keys(v);
  return [];
}
keysOf(null); // TypeError
  1. Write typeof v == 'object'
  2. Also require v !== null
  3. Use Object.entries(v) instead
  4. Check typeof v !== 'undefined'
typeof null is 'object' (a historic quirk), so null passes the check and Object.keys(null) throws. Rule null out explicitly.
Play Problem Solving ▸