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

Which fix removes the bug?

const el = document.querySelector('.box')!;
el.classList.add('on');
  1. Guard it: if (el) el.classList.add('on')
  2. Guard it: if (el!) el.classList.add('on')
  3. Change querySelector to getElementById
  4. Cast the result to as HTMLElement first
The ! only silences the type error; at runtime a missing element still throws.
Play Problem Solving ▸