Javascript

From clawic/skills · View interactive version

Javascript is an AI agent skill from the GitHub repository clawic/skills. Writes, debugs, and reviews JavaScript: async and the event loop, coercion, closures, dates, Unicode, regex, and modern ES2023+ APIs. Use when JS throws TypeError or "undefined is not a function", a promise never settles or a rejection goes unhandled, NaN or [object Object] appears, dates shift by a

How to use this skill

Skill file

User preferences and memory live in ~/Clawic/data/javascript/ (see setup.md on first use, memory-template.md for the file format). If you have data at an old location (~/javascript/ or ~/clawic/javascript/), move it to ~/Clawic/data/javascript/, and say in one line that you moved it and from where.

Configuration

User-dependent variables. Defaults apply until the user states a preference; store them in ~/Clawic/data/javascript/config.yaml.

| Variable | Type | Default | Effect |

|---|---|---|---|

| runtime_target | node \| browser \| both | node | Selects which platform file applies by default (node.md vs browser.md) and which floor gates advice |

| node_floor | number (Node major) | from package.json engines, else 22 | Gates every recommendation against the feature-floor table in modern.md; flags any API above the floor |

| module_system | esm \| cjs \| dual | esm | Switches module guidance and import/export examples in modern.md; dual activates the dual-package hazard checks |

| browser_floor | text (e.g. "Safari 16+", "last 2 years") | none | When set, gates syntax and API advice for browser-targeted code the way node_floor gates Node |

Preference areas to record as the user reveals them:

When To Use

Quick Reference

| Situation | Go to |

|---|---|

| TypeError/ReferenceError, NaN appearing, value undefined after await, works-in-dev-only, heisenbug | debug.md |

| Promise/await bug, rejection handling, cancellation, concurrency limits, races | async.md |

| Throwing, catching, custom errors, cause chains, global error hooks, serializing errors | errors.md |

| == surprise, truthiness, implicit conversion, ?? vs \|\| | coercion.md |

| Array/Object/Map/Set choice, copying, sorting, iteration traps | collections.md |

| ES2020+ syntax semantics, feature floors, modules (ESM/CJS), classes, generators/iterators | modern.md |

| Memory grows, listener/timer leaks, WeakMap/WeakRef, heap snapshots | memory-leaks.md |

| Slow code, jank, benchmarks, GC pressure, workers | performance.md |

| Regex wrong matches, stateful lastIndex, catastrophic backtracking, Unicode flags | regex.md |

| JSON precision loss, Date/Map round-trips, reviver/replacer, canonicalization | json.md |

| Env vars, process exit, signals, streams, Buffer, child processes | node.md |

| DOM events, storage, script loading, fetch response handling | browser.md |

| Anything else (numbers, dates, strings, this, timers) | Sections below |

Core Rules

  1. === always; the only defensible == is the idiom x == null — it matches both null and undefined in one check. Never expand it to two comparisons.
  2. Float equality is a band, not ===: Math.abs(a - b) <= Number.EPSILON * Math.max(1, Math.abs(a), Math.abs(b)). Worked: 0.1 + 0.2 - 0.3 ≈ 5.6e-17, inside the band (EPSILON ≈ 2.2e-16). Money never touches floats: integer minor units.
  3. Every promise gets a handler attached synchronously — a rejection with no handler when the microtask queue drains crashes Node (default since Node >=15) and fires unhandledrejection in browsers.
  4. Mutation is opt-in: default to toSorted/toReversed/with and spread; structuredClone only when depth is real. Runtime floors for all of these: modern.md.
  5. Durations from performance.now() (monotonic); wall-clock timestamps from Date.now(). Never subtract two Date.now() calls for benchmarks — NTP can step it backwards mid-measurement.
  6. Sequential vs parallel is a decision you write down: for...of + await = sequential; Promise.all(arr.map(f)) = parallel, fail-fast, and it does NOT cancel the losers.
  7. .length counts UTF-16 code units, not characters: "😀".length === 2. Slice user-visible text with Intl.Segmenter, never by index.
  8. Check the feature-floor table in modern.md before shipping ES2023+ APIs — one toSorted call breaks Node 18 at runtime, not at build time.

Numbers & Money

Dates & Time

Strings & Unicode

Objects, this & Closures

Timers & the Event Loop

Traps

| Trap | Why it fails | Do instead |

|---|---|---|

| forEach(async x => ...) | forEach ignores returned promises: everything runs at once, "finishes" instantly | for...of (sequential) or Promise.all(arr.map(f)) |

| Array(3).fill({}) | one shared object, three references | Array.from({length: 3}, () => ({})) |

| return fetchThing() inside try | the rejection settles after try exits — catch never sees it | return await fetchThing() |

| Promise.race([op, timeout]) | the loser keeps running and holding sockets/memory | AbortSignal.timeout(ms) passed into the op |

| return/throw inside finally | overrides the try's result and swallows its exception | finally is for cleanup only |

| throw "failed" | no stack, instanceof Error false, breaks error middleware | new Error("failed", {cause: err}) |

| JSON.parse(JSON.stringify(x)) as deep clone | drops undefined/functions, Date → string, Map/Set → {}, throws on cycles | structuredClone(x) |

| obj.fn?.() when fn is a number | ?. guards null/undefined only, not "not callable" | typeof obj.fn === "function" && obj.fn() |

| if (x) for "is x set" | silently drops 0, "", false | x != null |

| setInterval around async work | next run starts while the previous still awaits — overlapping executions | chained setTimeout re-armed after each completion |

| innerHTML with user text | the text executes as markup — XSS | textContent, or one sanitizer at the render boundary |

Output Gates

Before emitting JS code or a review verdict, verify:

Where Experts Disagree

Related Skills

More Clawic skills, get them at https://clawic.com/skills/javascript (install if the user confirms):

Feedback

Part of Clawic, the verified skill library. Get this skill: https://clawic.com/skills/javascript.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.