JavaScript debugging exercises
JavaScript is forgiving in the worst way: read past the end of an array and you get undefined, add a number to a string and you get text, forget an await and you get a Promise. The program keeps running and the bug surfaces somewhere else. These exercises train you to trace that kind of failure back to its cause.
Each one is a short function with one bug, the symptom it produces, and tests that pass only once it is fixed. They run in a sandboxed worker in your browser, from array bounds and missing returns up to closures and async race conditions.
How to debug a JavaScript function
- Find where the wrong value first appears. undefined and NaN travel: log the inputs and each intermediate value until you reach the first one that is already wrong.
- Check the types, not just the values. typeof and Array.isArray tell you whether "5" is a string, and whether that list is really an array.
- Read the error message literally. "Cannot read properties of undefined (reading 'name')" means the thing before .name was undefined, not name itself.
- For async code, check what is actually awaited. A function that returns a Promise you never awaited finishes later than everything that depends on it.
- Rerun every test after the fix. If another case breaks, the fix was in the wrong place.
The exercises
Easy
- Dashboard crashing on missing dataNull and undefined
- Validation returning reversed resultsLogic
- Cart total comes out as gibberishType
- First slice is missing dataOff-by-one
- Error when fetching names from empty groupsNull and undefined
- Shipping discount applied incorrectlyLogic
- Words start with duplicate lettersOff-by-one
- Helper function returning undefinedAsync and race conditions
- Multiplier running foreverInfinite loops
- Array iteration throwing out of boundsOff-by-one
- Number sort puts 10 before 2Logic
Medium
- Function crashing on return statementScope and closure
- List truncator missing the final itemOff-by-one
- Score calculation breaks on bad dataNull and undefined
- String utility crashes the browserInfinite loops
- Sorting numbers alphabeticallyType
- Variable not found in scopeScope and closure
- Vote tally returns massive numbersType
- Array mapping returns empty objectsAsync and race conditions
- Total calculation logs object instead of valueAsync and race conditions
- Financial totals slightly inaccurateMutation and copying
- Deeply nested arrays not flatteningMutation and copying
- Data groups overwriting each otherLogic
- Total ignores the quantity of each itemLogic
- API aggregator dropping responsesAsync and race conditions
- Delayed function returning undefinedAsync and race conditions
- Array contains pending operationsAsync and race conditions
- ReferenceError crashing the appScope and closure
- Counter stuck at zeroScope and closure
- Loop hanging on specific conditionsInfinite loops
- Parser getting stuck on mismatchesInfinite loops
- Validation passing empty stringsType
- Number conversion rounding incorrectlyType
- Zero values being ignoredNull and undefined
- Function crashing when item not foundNull and undefined
- Formatter appending extra charactersOff-by-one
- Math formula returning negative resultsLogic
- Formatter leaving unwanted charactersMutation and copying
- Mapping strings to numbersType
- AI Attempt: Remove all occurrencesLogic
Hard
- Function returning undefined prematurelyAsync and race conditions
- Set cleaner gets stuck in a loopInfinite loops
- Event listeners logging identical valuesScope and closure
- Process completes before data arrivesAsync and race conditions
- Sequential data arriving out of orderAsync and race conditions
- Reducer outputting invalid stringsAsync and race conditions
- Errors bypassing the catch blockAsync and race conditions
- High-traffic vote counters returning low totalsAsync and race conditions
- Expensive database queries duplicatingAsync and race conditions
- System allowing duplicate reservationsAsync and race conditions
- Variable returning unexpected valuesScope and closure
- Class method throwing undefined errorScope and closure
- Multiple instances incrementing same valueScope and closure
- Search algorithm freezing indefinitelyInfinite loops
- Type checker failing on null inputsType
- Equality check failing for identical dataType
- Null values bypassing defaultsNull and undefined
- Deep object access crashingNull and undefined
- Pagination starting on wrong pageOff-by-one
- Filter returning too many resultsLogic
- Addition formula returning massive numbersMutation and copying
- AI Attempt: The English ListMutation and copying
What you will practise
Questions
Does this need Node.js?
No. Your code runs in a sandboxed Web Worker in the browser, isolated from the page.
Which JavaScript do the exercises use?
Plain modern JavaScript: no frameworks, no build step. The bugs are the ones you meet in any codebase, React or Node included.
Is there a certificate?
Yes, free. Fixing 15 easy JavaScript bugs while signed in earns JavaScript Debugging · Level 1, with a verification link for LinkedIn.