bugger.
JAVASCRIPT / PRACTICAL GUIDE

JavaScript Debugging: Async, Date, and Prototype Bugs

JavaScript often keeps running after an invalid assumption instead of crashing. These guides and free interactive challenges isolate real JavaScript semantics—enumeration order, Unicode slicing, calendar carry, async backpressure, and prototype boundaries—so you can spot them quickly in production code.

What causes the hardest JavaScript runtime bugs?

Many subtle JavaScript bugs happen without throwing an error because of implicit language rules: object integer-key sorting, UTF-16 code units vs characters, Date month rollovers, shared async sources, and inherited prototype properties.

  • Object keys that look like array indexes enumerate in numeric order before insertion order.
  • String.length counts UTF-16 code units, splitting surrogate-pair emoji and non-BMP characters in half.
  • setFullYear or Date arithmetic silently overflows months (Feb 29 + 1 year becomes March 1 or 28 depending on API).
  • A cold async generator or shared promise can be consumed twice or exceed concurrency caps when pending reads are untracked.
  • Merging untrusted objects without checking Object.hasOwn can read or mutate Object.prototype.

1. Separate product sorting from object key order

ECMAScript OwnPropertyKeys iterates non-negative integer index keys in ascending numeric order before string keys in insertion order. Using a plain object to deduplicate or store timestamped buckets can reorder negative or string keys unexpectedly.

When an API promises chronological or insertion order, use a Map or sort explicitly with a numeric comparator instead of relying on plain-object key iteration.

2. Match String.length to the real counting unit

In JavaScript, String.prototype.length and slice() operate on 16-bit UTF-16 code units, not Unicode scalar values or grapheme clusters. Truncating a user name at index 10 can slice an emoji in half and produce an invalid surrogate.

Use Array.from(text) or Intl.Segmenter when the product contract counts characters or graphemes, and keep validation and truncation on the same unit.

JavaScript example
const name = "Ada🚀";
console.log(name.length); // 5 UTF-16 code units
console.log(Array.from(name).length); // 4 Unicode scalar values

3. Reserve async capacity before awaiting

In concurrent pumps and work queues, checking activeWorkers < limit after an await creates a window where multiple iterations pass the check before any increments the counter. Similarly, observing a cold async iterable before passing it on can consume the stream.

Reserve capacity synchronously before yielding at await, normalize or tee shared async sources once, and release slots in a finally block so errors never leak concurrency budget.

  • Reserve concurrency slots synchronously before any await.
  • Normalize cold vs hot async sources once before sharing.
  • Guard recursive object merges with Object.hasOwn and null-prototype objects.

Frequently asked questions

Why does Object.keys reorder numeric keys in JavaScript?
The ECMAScript specification requires OwnPropertyKeys to return array-index keys ("0", "1", "2") in ascending numeric order first, followed by remaining string keys in insertion order. Use a Map when insertion order must hold for all keys.
How do you prevent prototype pollution when merging JavaScript objects?
Check Object.hasOwn(source, key), skip __proto__, constructor, and prototype keys, and avoid mutating inherited properties on target objects.

Further reading

Start with primary reports and documentation. Community discussions are discovery leads, not evidence on their own.

PUT THE METHOD TO WORK

Practice on a focused challenge.

Explore JavaScript challenges
BUILD A REGULAR PRACTICE

Start free. Keep following the evidence.

Explore the current challenge collection, or see what is planned for future practice packs.