When to Use
User needs JavaScript expertise — from core language features to modern patterns. Agent handles async/await, closures, module systems, and ES2023+ features.
Quick Reference
| Topic | File |
|---|---|
| Async patterns | async.md |
| Type coercion rules | coercion.md |
| Array and object methods | collections.md |
| Modern ES features | modern.md |
Equality Traps
==coerces:"0" == falseis true — use===alwaysNaN !== NaN— useNumber.isNaN(), not=== NaNtypeof null === "object"— check=== nullexplicitly- Objects compare by reference —
{} === {}is false
this Binding
- Regular functions:
thisdepends on call site — lost in callbacks - Arrow functions:
thisfrom lexical scope — use for callbacks setTimeout(obj.method)losesthis— use arrow or.bind()- Event handlers:
thisis element in regular function, undefined in arrow (if no outer this)
Closure Traps
- Loop variable captured by reference —
letin loop or IIFE to capture value varhoisted to function scope — creates single binding shared across iterations- Returning function from loop: all share same variable — use
letper iteration
Array Mutation
sort(),reverse(),splice()mutate original — usetoSorted(),toReversed(),toSpliced()(ES2023)push(),pop(),shift(),unshift()mutate — spread[...arr, item]for immutabledelete arr[i]leaves hole — usesplice(i, 1)to remove and reindex- Spread and
Object.assignare shallow — nested objects still reference original
Async Pitfalls
- Forgetting
awaitreturns Promise, not value — easy to miss without TypeScript forEachdoesn't await — usefor...offor sequential asyncPromise.allfails fast — one rejection rejects all, usePromise.allSettledif need all results- Unhandled rejection crashes in Node — always
.catch()or try/catch with await
Numbers
0.1 + 0.2 !== 0.3— floating point, use integer cents ortoFixed()for displayparseInt("08")works now — butparseInt("0x10")is 16, watch prefixesNumber("")is 0,Number(null)is 0 — butNumber(undefined)is NaN- Large integers lose precision over 2^53 — use
BigIntfor big numbers
Iteration
for...initerates keys (including inherited) — usefor...offor valuesfor...ofon objects fails — objects aren't iterable, useObject.entries()Object.keys()skips non-enumerable —Reflect.ownKeys()gets all including symbols
Implicit Coercion
[] + []is""— arrays coerce to strings[] + {}is"[object Object]"— object toString{} + []is0in console —{}parsed as block, not object"5" - 1is 4,"5" + 1is "51" — minus coerces, plus concatenates
Strict Mode
"use strict"at top of file or function — catches silent errors- Implicit globals throw in strict —
x = 5without declaration fails thisis undefined in strict functions — not global object- Duplicate parameters and
withforbidden