Storage and `this` in JavaScript: The Two Interview Topics That Trip Up Every Front-End Candidate
Misunderstanding the storage hierarchy leads to architectures that either pound the database unnecessarily or leak sensitive data into browser-accessible stores. Getting `this` wrong produces bugs that survive code review because the function looks correct where it's defined — the error only surfaces at a different call site.
Server-side storage follows a tiered chain: MySQL holds the source of truth, Redis sits in front as a hot cache, and the browser chips in with its own cache and localStorage for offline persistence. The whole point is to avoid hammering the database on every read. On the client, localStorage acts as a tiny, same-origin key-value store limited to about 5MB and strings only — forget to JSON-serialize an object and you get `[object Object]`.
`this` in JavaScript is determined entirely by the call site. A regular function call points to `window` (or `undefined` in strict mode); a method call points to the object before the dot; a constructor call points to the new instance; an event handler points to the element that fired the event; and `call`/`apply`/`bind` let you override all of that. The most common pitfall is reference assignment: pulling a method off an object and calling it bare loses the original binding.
`var` declarations leak onto `window`, which creates a subtle trap inside `setTimeout` callbacks — a regular function there finds `window.name` instead of the object's property. Arrow functions fix this by inheriting `this` from the enclosing lexical scope, and they ignore any attempt to rebind with `call` or `apply`.
The storage hierarchy mirrors backend patterns that have been standard for a decade, but front-end developers often encounter them only as interview trivia rather than as a design tool they reach for daily.
Reference assignment breaking `this` is not a quirk — it follows directly from the rule that `this` is call-site determined, yet it remains the single most common source of binding bugs in JavaScript codebases.
`var` polluting `window` is a legacy behavior that still bites in codebases mixing old scripts with modern modules; the interaction with `setTimeout` is a particularly nasty edge case because the bug is silent until a global variable happens to shadow an object property.