LearnThatStack Ace your next interview

JavaScript

ES2022, as in current browsers and Node 18+ Checked Sep 2026 119 code snippets

The model

  • Scope is lexical, set by where code is written. A closure keeps outer variables alive after their function returns.
  • this is set by how a function is called, not where it's written. Arrow functions take this from the code around them.
  • Objects inherit through a prototype chain. If an object lacks a property, JavaScript checks each prototype in turn, up to null.
  • A variable holds a primitive or a reference to an object. A function gets a copy of that value, never the object itself.
  • Code runs on one thread. Async work queues a callback, and the event loop runs it once the call stack is empty.
Call stackruns your codeMicrotask queuepromise callbacksTask queuetimers, eventsWeb APIstimers, network, DOMQUEUEsetTimeout, fetchtimer fires queue its callback.then(fn) when it settlesRUN1. run them all2. run one task then repeat
When the call stack empties, every waiting microtask runs before the next task. So a .then callback beats a zero-delay setTimeout.

Variables & types

declare values and check what they hold

Functions & scope

closures, this, and functions as values

Objects & classes

build objects, share methods, lock them down

Copy, compare and pass objects

Copy the top level{ ...obj }
Copy every levelstructuredClone(obj)
Deep copy, the old wayJSON.parse( JSON.stringify(o) )
Compare two objectsa === bSame object only
Compare by contentsShallow checks top-level keys, deep recurses
Pass one to a functionIt gets a copy of the reference

TrapA spread copy shares nested objects with the original.

Arrays, loops & syntax

loop, transform and unpack

Array methods

Transform each itemarr.map(x => x * 2)
Keep matching itemsarr.filter(x => x > 2)
Combine into one valuearr.reduce((a, x) => a + x, 0)
Loop for side effectsarr.forEach(fn)Returns undefined
Copy part of an arrayarr.slice(1, 3)
Remove or insert in placearr.splice(1, 2, x)Returns what it removed

Trapreduce with no start value throws on an empty array.

Async & the event loop

promises, await, and what runs when

Errors & strict mode

make mistakes throw, then catch them

DOM & events

find elements, change them, react to input

Change the page

Create and add an elementdocument.createElementparent.append(el)
Set text safelyel.textContent = s
Set HTMLel.innerHTML = sNever pass it user input
Read only the visible textel.innerTextSlower, because it forces a layout

TipThe value attribute keeps the HTML's first value. The value property holds what the user typed.

Browser APIs

storage, network, lazy loading and workers

Modules & builds

split code into files and ship it

Performance & patterns

fast pages, code easy to change

Debounce, throttle and memoize

eventsdebouncewaitrunsthrottlewaitwaitwaitrunsrunsruns

Debounce runs after the events stop. Throttle runs at the first event, then skips events until its wait ends.

Wait until input stopsDebounce, for a search box
Cap how often it runsThrottle, for scroll handlers
Cache resultsMemoize. Pure functions only

TipFor deep recursion, use a loop. Only Safari optimizes tail calls.

Found this useful? Pass it on.
Pro · $10/mo

This sheet stays free. With Pro, you can:

  • Every answer on this sheet. Read the full answer to all 126 questions it links, not just the 16 free ones.
  • My sheet. The cards you star and the ones you miss in Quiz me, from every sheet, on one page to quiz and print.
  • PDFs. Download this sheet and My sheet, laid out for A4, with every question a link to its answer.
  • The rest of Pro. Every answer in the question library, and 1,000 AI credits a month.