Top 100 JavaScript Interview Questions & Answers
1. What is JavaScript?
-> JavaScript is a client-side scripting language used to make web pages interactive.
2. Difference between JavaScript and Java?
-> JavaScript is interpreted and runs in browsers; Java is compiled and runs on JVM.
3. Is JavaScript single-threaded?
-> Yes, but it handles async tasks using the event loop.
4. Data types in JS?
-> String, Number, Boolean, Object, Null, Undefined, Symbol, BigInt.
5. typeof operator?
-> Used to find the type of a variable.
6. What is NaN?
-> NaN stands for 'Not a Number' - result of invalid math operations.
7. Difference between == and ===?
-> == checks value; === checks value and type.
8. Truthy and falsy values?
-> Falsy: 0, '', null, undefined, false, NaN; everything else is truthy.
9. Variable hoisting?
-> Declarations are moved to the top before execution.
10. let, const, var difference?
-> var is function-scoped; let/const are block-scoped; const cannot be reassigned.
11. Template literals?
-> Strings with backticks allowing expressions like `Hello ${name}`.
12. Block scope?
-> Variables declared inside {} cannot be accessed outside.
13. Function declaration vs expression?
-> Declaration is hoisted, expression is not.
14. IIFE?
-> Immediately Invoked Function Expression `(function(){})();`.
15. Strict mode?
-> 'use strict' makes JS safer by catching silent errors.
16. Closure?
-> A function that remembers variables from its outer scope.
17. Lexical scope?
-> Scope defined by where variables are written in the code.
18. Local vs global scope?
-> Local variables inside functions; global accessible everywhere.
19. Recursion?
-> A function that calls itself until a condition is met.
20. First-class functions?
-> Functions can be treated as variables and passed as arguments.
21. Higher-order functions?
-> Functions that take or return other functions.
22. Arrow functions?
-> Shorter syntax and no own 'this' binding.
23. Callback function?
-> A function passed into another function to be executed later.
24. What is the event loop?
-> Mechanism that manages async operations in JS.
25. Microtasks vs Macrotasks?
-> Microtasks (Promises) run before Macrotasks (setTimeout).
26. Promise?
-> An object representing a future value or failure of an async operation.
27. async/await?
-> Syntactic sugar for Promises, makes async code look synchronous.
28. Callback Hell?
-> Nested callbacks that make code messy; solved by Promises/async-await.
29. What is a prototype?
-> Object from which other objects inherit properties.
30. What is prototypal inheritance?
-> Objects inherit directly from other objects.
31. What is 'this'?
-> Refers to the object that owns the current function.
32. call(), apply(), bind()?
-> Methods to manually set 'this'. call/apply run now; bind returns new function.
33. Difference between null and undefined?
-> undefined: declared but not assigned; null: intentional empty value.
34. Difference between slice and splice?
-> slice() copies; splice() removes/adds in place.
35. What is JSON?
-> JavaScript Object Notation - text format for structured data.
36. typeof null?
-> Returns 'object' (a JS bug since early versions).
37. What are promises states?
-> Pending, Fulfilled, Rejected.
38. What is async function?
-> Function returning a Promise, can use await inside.
39. What is hoisting?
-> JS moves declarations to top before execution.
40. Difference between map and forEach?
-> map returns new array; forEach doesn't.
41. Difference between var and let?
-> var: function-scoped; let: block-scoped.
42. Difference between shallow and deep copy?
-> Shallow copies references; deep copies all nested values.
43. Event bubbling?
-> Event propagates from inner to outer elements.
44. Event capturing?
-> Event travels from root to target (opposite of bubbling).
45. What is debounce?
-> Delays function execution until user stops triggering event.
46. What is throttle?
-> Executes function at fixed intervals, ignoring rapid triggers.
47. DOM?
-> Document Object Model - tree structure of webpage elements.
48. What is fetch API?
-> Modern method to make network requests returning Promises.
49. localStorage vs sessionStorage?
-> localStorage persists; sessionStorage clears after tab closes.
50. What are modules?
-> Reusable pieces of JS code (import/export).
51. Difference between import and require?
-> import is ES6 syntax; require is CommonJS.
52. Pure function?
-> Same input gives same output, no side effects.
53. Impure function?
-> Output can change or affect outside variables.
54. Mutable vs Immutable?
-> Mutable changes data; Immutable creates new data.
55. Spread operator?
-> Expands elements: `[...arr1, ...arr2]`.
56. Rest parameter?
-> Collects all arguments: `function f(...args){}`.
57. Destructuring?
-> Extracting values from arrays/objects into variables.
58. NaN === NaN?
-> False - NaN is not equal to itself.
59. What is 'use strict'?
-> Enables strict mode for cleaner, safer code.
60. Temporal Dead Zone?
-> Time between variable declaration and initialization using let/const.
61. What are generators?
-> Functions that can pause and resume using `yield`.
62. Symbol type?
-> Unique primitive value often used as object keys.
63. What is BigInt?
-> Used for integers larger than Number can handle.
64. What is typeof NaN?
-> Returns 'number'.
65. Difference between deep copy and shallow copy?
-> Deep copies nested values; shallow copies references.
66. setTimeout vs setInterval?
-> setTimeout runs once; setInterval repeats.
67. clearTimeout?
-> Cancels a timeout before it runs.
68. What is [Link]?
-> Waits for all Promises to resolve, rejects if one fails.
69. [Link]?
-> Waits for all Promises, regardless of success/failure.
70. [Link]?
-> Resolves with first fulfilled Promise, ignores rejections.
71. Event delegation?
-> Handling events at parent instead of multiple child elements.
72. Arrow function vs normal function?
-> Arrow has no 'this' or 'arguments' binding.
73. What is an iterator?
-> Object with next() method returning {value, done}.
74. What is async iteration?
-> Uses 'for await...of' to loop over Promises.
75. Difference between call stack and heap?
-> Stack stores function calls; heap stores objects.
76. What is garbage collection?
-> Automatic memory cleanup for unreferenced data.
77. Difference between undefined and undeclared?
-> undefined: declared but not assigned; undeclared: never defined.
78. What is eval()?
-> Executes a string as JS code (not recommended).
79. What are async generators?
-> Generators that can yield Promises using 'for await...of'.
80. What is an event listener?
-> A function that runs when an event occurs.
81. What is the DOMContentLoaded event?
-> Fires when the HTML is fully loaded, before images.
82. innerHTML vs textContent?
-> innerHTML parses HTML; textContent shows plain text.
83. [Link]()?
-> Converts JS object to JSON string.
84. [Link]()?
-> Converts JSON string to JS object.
85. Difference between deep and shallow copy?
-> Deep copies nested objects; shallow only references.
86. What is a promise chain?
-> Chaining multiple .then() calls for sequential async actions.
87. async/await error handling?
-> Use try...catch block.
88. What is a module bundler?
-> Tool like Webpack or Rollup that combines JS files.
89. Event loop priority?
-> Synchronous -> Microtasks -> Macrotasks -> Rendering.
90. What is reflow and repaint?
-> Reflow recalculates layout; repaint updates visuals.
91. Explain 'this' in arrow functions?
-> Arrow functions inherit 'this' from parent scope.
92. Explain destructuring assignment?
-> Breaking complex structures into variables.
93. What is the difference between shallow and deep equality?
-> Shallow checks references; deep checks actual values.
94. What is short-circuit evaluation?
-> Stops evaluation if first condition decides result (like in && or ||).
95. What is currying?
-> Breaking a function into multiple single-argument functions.
96. What are polyfills?
-> Code that adds new features in old browsers.
97. What is a promise race condition?
-> When async tasks finish unpredictably, results may vary.
98. What are web workers?
-> Run JS in background threads for heavy tasks.
99. What is async defer in script tag?
-> async loads script while parsing; defer runs after parsing.
100. What is an Immediately Invoked Async Function?
-> An async function that runs right away: (async ()=>{})();