Javascript
161 bites tagged Javascript — interview questions with model answers, and 60-second explainers.
Promise .catch() versus async/await try...catch
.catch() handles rejection for all preceding chain steps and reads functionally; try/catch reads synchronously and can scope errors per await, but only catches awaited rejections. Async error-handling models.
Understanding this in TypeScript
In TypeScript, this is determined by how a function is called, not where it is defined; arrow functions capture the enclosing this lexically, and TypeScript adds optional this parameters to type-check the expected context at compile time.
Prototype pollution: how it works and prevention
Attacker writes to Object.prototype via __proto__ keys in merge/parse code, poisoning all objects; prevent by guarding keys, null-prototype objects, Object.freeze, Map, and patched deps. Deep JS object-model security.
Async iterators and for await...of for streaming
Async iterators yield values lazily over time; for await...of consumes them sequentially with backpressure, keeping memory bounded. streaming vs buffering everything.
Promise.all vs Promise.allSettled
All rejects on the first failure; allSettled always fulfills with a status/value or reason per input. Use allSettled when partial success is acceptable. choosing fail-fast vs collect-all.
Bounded concurrency for many async requests
Chunk the array and await Promise.all per chunk, or run a fixed worker pool pulling from a shared index; cap in-flight requests. limiting concurrency, not just running parallel. firing all 1000 at once or going fully serial.
Comparing the three async error-handling styles
Callbacks pass err as first arg; Promises route errors to catch; async/await uses try/catch; an unhandled rejection can crash the Node process. fluency across async error styles.
Running independent requests with Promise.all and race
Start all requests then await Promise.all to get all results or fail fast on first rejection; use Promise.race when only the fastest settled result matters. concurrent Promise combinators.
Output order of sync, microtask, and macrotask
C runs first synchronously, then B drains from the microtask queue, then A from the macrotask queue. microtask vs macrotask priority. claiming setTimeout(0) beats the Promise, or that order is random.
The three states of a JavaScript Promise
Pending, fulfilled, rejected; settle is one-way and final; create with the executor calling resolve or reject, consume with then and catch. fundamentals of Promise lifecycle.
Track scroll depth: Intersection Observer vs scroll events
Place sentinels at depth thresholds and fire once via Intersection Observer, off the main thread; scroll listeners fire constantly and need throttling. efficient scroll-depth tracking.
SvelteKit 2.61 breaks remote functions, adds live queries
SvelteKit 2.61 removes .run() from remote queries and rewrites enhance callbacks, forcing refactors. New query.live() adds async-iterable real-time streams. If you use remote functions, check three breaking changes before upgrading.
Vue 3.4 Cuts Build Times 44%, Stabilizes defineModel
Vue 3.4 cuts SFC compile times 44% and doubles parser speed while stabilizing defineModel for v-model. The refactored reactivity engine skips redundant computed triggers, reducing re-renders. Upgrade requires Volar 1.8.27+ and matching toolchain versions.
Vue 3.5 cuts reactivity memory 56%, adds lazy hydration
Vue 3.5 drops reactivity memory 56% and large arrays up to 10x faster with no breaks. Stable reactive props destructure kills withDefaults boilerplate, while lazy hydration and useId() fix SSR pain points.
How would you use dynamic import() to lazy-load a component-specific library?
Tests lazy loading and bundle splitting. Good answers call import() inside the component lifecycle, await the module namespace, let the bundler split the chunk, and handle loading and error states. Bad answers put import() at top level, defeating lazy loading.
How do you update Svelte arrays and objects to trigger reactivity?
This tests knowledge of Svelte 5 deep reactivity: $state wraps arrays in proxies so push and property mutations trigger updates. Mention $state first, then note legacy syntax needs reassignment. A red flag is claiming push never works in Svelte.
Fix keyboard trap in a custom modal dialog
Tests accessible modal focus management. Strong answers: role="dialog" and aria-modal="true", move focus on open, trap Tab/Shift+Tab cycles inside, Escape to close, return focus to trigger. Red flag: CSS-only fixes or aria-hidden without JS focus control.
Chrome 136 cuts JS startup 630ms with compile hints
Chrome 136 ships Explicit Compile Hints, cutting parse and compile times by 630ms in V8 tests. The //# allFunctionsCalledOnLoad comment triggers eager background compilation for entire files, avoiding main-thread bottlenecks.
V8 doubles JSON.stringify speed with side-effect-free fast path
V8 doubled JSON.stringify speed with a side-effect-free fast path. Every network and storage serialization of plain objects gets faster with zero code changes. Check your profiles if JSON encoding shows up hot.
Type a UMD library for script tags and CommonJS/ESM
Tests TypeScript UMD declarations for dual global and module usage. Use export as namespace MyLib for the global, export = MyLib for require, and declare the API inside a namespace. Red flag: default exports or declare global instead of export as namespace.
Explain the difference between microtask and macrotask queues
Macrotasks like setTimeout run one per tick; microtasks like Promises drain fully after each task before rendering. Your event loop mental model and starvation hazards.
What is the exact output and event sequence of this code?
Tests event loop priority between sync, microtasks, and macrotasks. A strong answer gives Start, End, Promise, Timeout; explains Promise.then is a microtask and setTimeout is a macrotask, microtasks draining before the next macrotask.
How do you add TypeScript types for a library without bundled types?
This tests the DefinitelyTyped @types workflow. Install @types/package as a dev dependency via npm or yarn, skipping it when the library already bundles .d.ts files. A red flag is suggesting manual declarations or extra tooling before checking DefinitelyTyped.
Which Web API offloads expensive work from the main UI thread?
Cite Web Workers, instantiate new Worker(url), and communicate via postMessage and onmessage. knowledge of moving CPU-heavy work off the main thread. citing setTimeout or async/await, which still run on the main thread.
Get Javascript bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.