Member-only story
JavaScript Mistakes That Make Your Code Harder to Debug
Common problems with references, async code, coercion, and mutation.
After spending enough time tracking down bugs in JavaScript, I have found that sometimes the most difficult bugs are not the ones caused by complex code. Some of them result from a single incorrect assumption.
The array was supposed to stay unchanged, but it did change unexpectedly. The async function returned an expected value too late. A certain check is true for some inputs and false for others. Or even the variable values look valid in the debugger, while the application still misbehaves.
All these cases are annoying, since the code looks fine.
However, the good thing is that there are certain patterns in such bugs. Once you learn what really happens in JavaScript, you will recognize them before wasting days trying to fix.
This post will cover eight common mistakes when writing JavaScript code, which make it more difficult to debug, and provide examples of their fixes.
1. Assuming Objects Are Copied Automatically
One of the easiest ways to introduce a subtle bug is to misunderstand how JavaScript handles objects and arrays.








