Interview Q&A · JavaScript · 8 questions in this topic
Scenario-Based Questions — JavaScript Interview Q&A
Judgment questions — slow pages, broken fetches, and state bugs from real on-call rotations. Part of Scenarios & Rapid Fire · 204 questions in the JavaScript bank.
A page is slow to interact. How do you diagnose it?
Answer
Profile first (Performance panel: long tasks, JS vs render vs network), check bundle bytes and third-parties, look for hydration/render storms and unmemoized lists, then fix the measured bottleneck — split code, virtualize lists, debounce handlers, move work to workers.
Measure-first beats guess-memoizing every time.
Key points
- 1Profile — long tasks, flame chart.
- 2Bytes — bundle, vendors, images.
- 3Render — memoize, virtualize, composite-only.
💡 INP-plus-bundle is the first-two-checks pair.
Fetches fail intermittently in production. How do you harden them?
Answer
Timeouts (AbortController race), retries with backoff+jitter on transient failures, idempotency keys for unsafe methods, offline/empty/loading states in UI, and request logging with correlation ids. Never infinite-retry non-idempotent writes.
Timeout-plus-backoff-plus-states is the resilience triple.
Key points
- 1Bound — timeouts, attempt caps.
- 2Retry — transient-only, jittered backoff.
- 3Surface — loading/error/offline UI.
State updates do not re-render (stale UI). How do you debug it?
Answer
Check reference identity (mutated-in-place state is invisible to shallow compare), key stability in lists (index keys reshuffle state), and closure staleness (handlers capturing old state — functional setState or refs fix it).
Mutation-plus-stale-closure covers most stale-UI incidents.
Key points
- 1Identity — new references required.
- 2Keys — stable ids, not indices.
- 3Closures — functional updates, refs.
Example
// setList((prev) => [...prev, item]); // fresh ref + fresh closure💡 Functional-updates fix both mutation and staleness classes.
Search-as-you-type fires stale results (race). How do you fix it?
Answer
Abort prior requests (AbortController per keystroke), debounce input, and ignore-or-cancel out-of-order responses (sequence ids, only latest renders). The UI shows exactly the latest query's results.
Abort-plus-debounce-plus-latest-wins is the race-free trio.
Example
// let c; input.oninput = () => { c?.abort(); c = new AbortController(); // fetch(url, { signal: c.signal }).then(renderLatest); };
Memory grows over a session (suspected leak). How do you find it?
Answer
Snapshot-diff retained objects after releasing (three-snapshot technique), hunt detached DOM plus unremoved listeners/timers, check caches without eviction, and gate CI on allocation tests.
Retained-after-release diffing isolates the leak to an action.
Key points
- 1Diff — snapshot before/after actions.
- 2Suspects — detached DOM, listeners, timers.
- 3Prevent — bounded caches, cleanup paths.
How do you review a JavaScript pull request? What do you check first?
Answer
Correctness first (awaited promises, nullish handling, effect deps, key stability), then robustness (error paths, cleanup, a11y), then performance evidence (no N+1 fetches, memoized hots). Mechanical gates (lint, types, tests) green before human review.
Semantics-before-style is the reviewer credo.
Key points
- 1Correctness — async, nulls, deps, keys.
- 2Robustness — errors, cleanup, a11y.
- 3Performance — fetch counts, memo proof.
💡 Effect-deps-plus-keys catches the most React bugs in review.
How do you migrate legacy JS (var, callbacks, script tags) safely?
Answer
Strangler-style behind tests: modules per boundary, const/let codemods with TDZ review, promise-wrap callback cores, lint-plus-types as guardrails, and incremental route/page migration. Each step ships independently green.
Codemod-plus-tests-plus-increments is the safe-migration shape.
Key points
- 1Modularize — boundaries first.
- 2Modernize — syntax with tests.
- 3Async — promisify cores.
How do you handle secrets and auth tokens in frontend code?
Answer
Never ship secrets in client bundles (everything ships readable); session tokens in httpOnly+secure+sameSite cookies (XSS-proof storage); short-lived access tokens with refresh rotation; CSP plus sanitization against XSS exfiltration.
Client-is-public is the threat model — design like attackers read the bundle.
Key points
- 1Storage — httpOnly cookies for sessions.
- 2Lifetime — short access, rotated refresh.
- 3Defense — CSP, sanitized rendering.
💡 Bundles-are-public is the sentence that ends secret-in-frontend debates.
Recommended resourcesAffiliate links · may earn us a commission