JS Mistakes
A short tour of the JavaScript pitfalls that catch experienced engineers most often.
The classics
| Mistake | What happens | Fix |
|---|---|---|
== instead of === | Type coercion gives surprising results. | Always use === / !==. |
parseInt("08") without radix | 0 in some old engines (octal). | parseInt("08", 10). |
| 0.1 + 0.2 ≠ 0.3 | Float rounding. | Integer cents, toFixed, or a decimal library. |
Forgetting await | The Promise itself becomes the value. | Enable ESLint's no-floating-promises. |
Sequential await on independent calls | Slow. | Promise.all([a(), b()]). |
this inside a callback | Lost binding. | Arrow function or .bind(this). |
| Mutating shared state | Spooky action at a distance. | Spread, structuredClone, immutable libs. |
for…in on arrays | Iterates inherited keys. | for…of or array methods. |
| Date months from 1 | new Date(2026, 6, 1) is July. | Remember: months are 0-indexed. |
typeof null === "object" | Historic bug, can't change. | Test x === null explicitly. |
| Trailing comma in JSON | SyntaxError on parse. | Lint your JSON; producers should drop the comma. |
setTimeout inside loops with var | All closures share one variable. | Use let in the loop header. |
The "obvious in retrospect" ones
JS
// JSON.stringify drops undefined and functions silently
JSON.stringify({ name: "Ada", greet: () => "hi" });
// '{"name":"Ada"}'
// Array.length is a setter — shrinking truncates
const a = [1, 2, 3];
a.length = 1; // a is now [1]
// Spread on a string spreads characters
[..."ab"]; // ["a", "b"]
// Object spread is shallow
const u2 = { ...user };
u2.address.city = "X"; // u and u2 share the same address object
Tip: Add ESLint with the
eslint:recommended preset on day one. It catches a huge slice of this table before you commit.Example
Example
<!DOCTYPE html>
<html>
<body>
<p id="out"></p>
<script>
document.getElementById("out").textContent = "Hello from JS Mistakes!";
</script>
</body>
</html>
Try it Yourself »
Exercise
Pass an explicit radix to avoid the parseInt trap.
const n = parseInt(input,
);
A common base.
Discussion
Loading…