iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

JS Mistakes

A short tour of the JavaScript pitfalls that catch experienced engineers most often.

The classics

MistakeWhat happensFix
== instead of ===Type coercion gives surprising results.Always use === / !==.
parseInt("08") without radix0 in some old engines (octal).parseInt("08", 10).
0.1 + 0.2 ≠ 0.3Float rounding.Integer cents, toFixed, or a decimal library.
Forgetting awaitThe Promise itself becomes the value.Enable ESLint's no-floating-promises.
Sequential await on independent callsSlow.Promise.all([a(), b()]).
this inside a callbackLost binding.Arrow function or .bind(this).
Mutating shared stateSpooky action at a distance.Spread, structuredClone, immutable libs.
for…in on arraysIterates inherited keys.for…of or array methods.
Date months from 1new 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 JSONSyntaxError on parse.Lint your JSON; producers should drop the comma.
setTimeout inside loops with varAll 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, );

Test yourself

Q1. `typeof null` returns…
Q2. `Date` month numbering starts at…
Q3. `for…in` on arrays is…

Discussion

Loading…