# javascript & runtime
// [27] real interview questions. Answers and sources live in the practice app.
practice this topic- What is the event loop in JavaScript? What is the difference between the call stack and the task queue?(mid)
- Given `const foo = () => console.log('First'); const bar = () => setTimeout(() => console.log('Second')); const baz = () => console.log('Third');` and the calls `bar(); foo(); baz();` — what logs, in what order, and why?(junior)
- Explain the microtask queue. If you schedule `setTimeout(fn, 0)` and `Promise.resolve().then(fn2)` in the same script, which callback runs first and why?(mid)
- Inside a function you have `console.log(name); console.log(age); var name = 'Lydia'; let age = 21;` — what does each log print and why?(junior)
- What does `for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1); }` log, versus the same loop with `let i`? Why?(mid)
- What is a closure, and how/why would you use one?(junior)
- What are the pitfalls of closures in real applications? Give a concrete example of a closure causing stale state or a memory leak.(senior)
- Explain how `this` works in JavaScript. How did ES6 arrow functions change working with `this`?(mid)
- You have `function Person(first, last) {...}`, create `const member = new Person('Lydia', 'Hallie')`, then assign `Person.getFullName = function() {...}`. What happens when you call `member.getFullName()` and why? How do you fix it?(mid)
- What does `console.log(getData())` print when `async function getData() { return await Promise.resolve('I made it!'); }`? Why not the string?(mid)
- How is `Promise.all()` different from `Promise.allSettled()`? When would you pick each for parallel API calls?(mid)
- Two promises: one resolves 'one' via `setTimeout(res, 500, 'one')`, the other resolves 'two' via `setTimeout(res, 100, 'two')`. What does `Promise.race([firstPromise, secondPromise]).then(console.log)` print and why?(mid)
- How do you handle errors in asynchronous operations? Cover both promise chains and async/await, and what happens if you forget.(mid)
- Explain the differences between CommonJS modules and ES modules.(senior)
- How does JavaScript garbage collection work, and how can memory still leak in a garbage-collected language?(senior)
- In TypeScript, what is the difference between an `interface` and a `type` alias? When would you pick one over the other?(mid)
- What does the `any` type do in TypeScript, when is it acceptable, and why is `unknown` the safer alternative?(mid)
- Explain generics in TypeScript. Why is `function first<T>(arr: T[]): T` better than `function first(arr: any[]): any`?(mid)
- What are type guards in TypeScript? How does a discriminated union make narrowing safer?(mid)
- What does `keyof` do in TypeScript, and how do utility types like `Partial`, `Pick`, `Omit`, and `Record` build on it? Give a concrete use.(senior)
- What does `strictNullChecks` change in TypeScript, and how do you handle values that can legitimately be null?(mid)
- What are TypeScript enums, and why do many codebases prefer unions of string literals instead?(mid)
- Explain the difference between a shallow copy and a deep copy of a JavaScript object. How do you make each, and what are the traps?(junior)
- What is the difference between a `Map` and a plain object in JavaScript? When would you reach for a Map?(mid)
- What are iterators and generators in JavaScript, and what are they actually used for?(senior)
- Explain mutable vs immutable objects in JavaScript. What does `Object.freeze()` actually guarantee, and why does immutability matter in UI code?(mid)
- You lose Wi-Fi and a well-built web app immediately shows a 'you're offline' banner, then quietly syncs when you reconnect — without you refreshing. How does the page find out the network dropped and came back?(mid)