Scope, closures & this
What is a closure, and where would you use one?
A closure is a function that remembers variables from the scope where it was defined, even after that scope has returned. It's used for private state - a counter that keeps its own count, or a module that hides its internals.
var vs let vs const, and what is hoisting?
var is function-scoped and hoisted with an initial value of undefined. let and const are block-scoped and sit in a 'temporal dead zone' until declared, so accessing them early throws. const can't be reassigned (though objects it holds can still mutate).
How is 'this' determined?
By how a function is called: as a plain function (global/undefined in strict mode), as a method (the object before the dot), with new (the new instance), or via call/apply/bind. Arrow functions have no own 'this' - they inherit it from the enclosing scope.
The async model
Explain the event loop and what setTimeout(fn, 0) does.
JavaScript runs on a single thread. Synchronous code runs on the call stack; queued callbacks run only when the stack is empty. So setTimeout(fn, 0) doesn't run immediately - it runs after the current synchronous code finishes.
Callbacks vs promises vs async/await?
Promises flatten nested callbacks into chainable .then/.catch. async/await is syntactic sugar over promises that reads like synchronous code. Handle errors with .catch for promises and try/catch for async/await.
Microtasks vs macrotasks?
Promise callbacks are microtasks; timers and I/O callbacks are macrotasks. After each tick, the engine drains all microtasks before the next macrotask - which is why a resolved promise runs before a setTimeout(fn, 0).
Types, equality & ES6+
== vs ===?
=== compares value and type with no coercion; == coerces types before comparing, which causes surprises (e.g. 0 == '' is true). Prefer === unless you deliberately want coercion.
Explain destructuring, spread/rest and default parameters.
Destructuring unpacks values from arrays/objects into variables. Spread (...) copies or merges arrays/objects; rest (...) gathers remaining items into one. Default parameters give a fallback when an argument is undefined.
map vs filter vs reduce?
map transforms each element into a new array of the same length; filter keeps elements that pass a test; reduce folds the array into a single value (sum, object, etc.). Be ready to chain them.
Practice this now
