← All topics

JavaScript Middle interview questions and answers

75 questions for a JavaScript (Middle) interview - with correct answers, explanations and links to the docs.

Take this quiz
  1. 1.What does this code print?
  2. 2.How do call, apply and bind differ from each other?
  3. 3.What does this code print?
  4. 4.What does this code print after a second?
  5. 5.What does this code print?
  6. 6.How does the engine look up a property the object itself doesn't have?
  7. 7.What does Object.create(proto) do?
  8. 8.What are classes in JavaScript really?
  9. 9.Why must a subclass constructor call super() before accessing this?
  10. 10.How does #count differ from _count in a class?
  11. 11.How does obj instanceof Class work?
  12. 12.What does this code print?
  13. 13.What was an IIFE (function () { ... })() used for in the past?
  14. 14.What is currying?
  15. 15.How does debounce differ from throttle?
  16. 16.What is memoization?
  17. 17.How does Promise.all behave if one of the promises rejects?
  18. 18.What does Promise.allSettled return?
  19. 19.How does Promise.any differ from Promise.race?
  20. 20.How long will this code take if each request lasts 1 second?
  21. 21.Why doesn't forEach wait for async callbacks?
  22. 22.What does the chain print?
  23. 23.What does a promise's .finally() do?
  24. 24.In what order are the lines printed?
  25. 25.Why is a recursive setTimeout sometimes preferable to setInterval?
  26. 26.What is requestAnimationFrame for?
  27. 27.What does a generator function do?
  28. 28.What does your own object need to work with for...of and spread?
  29. 29.What is Symbol used for?
  30. 30.How does WeakMap differ from Map?
  31. 31.Why is structuredClone(obj) better than JSON.parse(JSON.stringify(obj))?
  32. 32.What does this code print?
  33. 33.What does Object.defineProperty(obj, 'id', { value: 1, writable: false }) give you?
  34. 34.What does [10, 1, 2].sort() return?
  35. 35.What do parseInt('12px') and Number('12px') return?
  36. 36.How do ES modules differ from CommonJS?
  37. 37.Why do you need dynamic import()?
  38. 38.Will fetch reject the promise on a 404 response?
  39. 39.How do you cancel a fetch request?
  40. 40.What is CORS?
  41. 41.Why is a cookie with the HttpOnly flag safer than a regular one?
  42. 42.How does stopPropagation differ from stopImmediatePropagation?
  43. 43.What does the { passive: true } option in addEventListener('touchmove', ...) do?
  44. 44.How do you efficiently insert 1000 elements into the DOM?
  45. 45.What's the most convenient way to detect that an element entered the viewport?
  46. 46.What's the right way to create a custom error class?
  47. 47.What does Object.fromEntries(Object.entries({ a: 1, b: 2 }).map(([k, v]) => [k, v * 10])) return?
  48. 48.How does a getter differ from a regular property?
  49. 49.What does Array.from({ length: 3 }, (_, i) => i * 2) return?
  50. 50.What does {} === {} return?
  51. 51.In what order are the lines printed?
  52. 52.Why does this catch not handle the rejection?
  53. 53.How does arr.toSorted() differ from arr.sort()?
  54. 54.What does "a-b-c".replace("-", "+") return?
  55. 55.Why are event handlers often written as arrow-function class fields?
  56. 56.What does this code print?
  57. 57.What does new Array(3).map((_, i) => i) return?
  58. 58.Which error does undefinedVariable.foo throw, and which null.foo?
  59. 59.What does typeof notDeclared return if notDeclared was never declared?
  60. 60.What happens when this code runs?
  61. 61.What does new User("Ann") do with a regular constructor function?
  62. 62.How does Object.assign(target, source) differ from { ...target, ...source }?
  63. 63.Why do you need two awaits here?
  64. 64.What is the easiest way to send all fields of a <form> with fetch?
  65. 65.How do you notify other parts of the page about something with a DOM event?
  66. 66.Which property should you use to detect the pressed key?
  67. 67.What does options.onSuccess?.(data) do?
  68. 68.What does config.timeout ??= 5000 do?
  69. 69.Why convert a large list of allowed IDs to a Set before many lookups?
  70. 70.How can a function keep private state without classes?
  71. 71.What does event.target.closest("[data-id]") return?
  72. 72.Why does el.style.width return an empty string for a width set in CSS?
  73. 73.How should money be stored to avoid 0.1 + 0.2 problems?
  74. 74.What does this code print?
  75. 75.What does arr.with(1, "x") return for arr = ["a", "b", "c"]?