# javascript & runtime

// [27] real interview questions. Answers and sources live in the practice app.

practice this topic
  1. What is the event loop in JavaScript? What is the difference between the call stack and the task queue?(mid)
  2. 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)
  3. 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)
  4. 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)
  5. What does `for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1); }` log, versus the same loop with `let i`? Why?(mid)
  6. What is a closure, and how/why would you use one?(junior)
  7. What are the pitfalls of closures in real applications? Give a concrete example of a closure causing stale state or a memory leak.(senior)
  8. Explain how `this` works in JavaScript. How did ES6 arrow functions change working with `this`?(mid)
  9. 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)
  10. What does `console.log(getData())` print when `async function getData() { return await Promise.resolve('I made it!'); }`? Why not the string?(mid)
  11. How is `Promise.all()` different from `Promise.allSettled()`? When would you pick each for parallel API calls?(mid)
  12. 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)
  13. How do you handle errors in asynchronous operations? Cover both promise chains and async/await, and what happens if you forget.(mid)
  14. Explain the differences between CommonJS modules and ES modules.(senior)
  15. How does JavaScript garbage collection work, and how can memory still leak in a garbage-collected language?(senior)
  16. In TypeScript, what is the difference between an `interface` and a `type` alias? When would you pick one over the other?(mid)
  17. What does the `any` type do in TypeScript, when is it acceptable, and why is `unknown` the safer alternative?(mid)
  18. Explain generics in TypeScript. Why is `function first<T>(arr: T[]): T` better than `function first(arr: any[]): any`?(mid)
  19. What are type guards in TypeScript? How does a discriminated union make narrowing safer?(mid)
  20. 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)
  21. What does `strictNullChecks` change in TypeScript, and how do you handle values that can legitimately be null?(mid)
  22. What are TypeScript enums, and why do many codebases prefer unions of string literals instead?(mid)
  23. 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)
  24. What is the difference between a `Map` and a plain object in JavaScript? When would you reach for a Map?(mid)
  25. What are iterators and generators in JavaScript, and what are they actually used for?(senior)
  26. Explain mutable vs immutable objects in JavaScript. What does `Object.freeze()` actually guarantee, and why does immutability matter in UI code?(mid)
  27. 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)