50+ JavaScript Interview Questions &
Answers (Topic-Wise)
1. Basics of JavaScript
Q1. What is JavaScript?
JavaScript is a high-level, dynamic programming language primarily used for adding interactivity to
web pages.
Q2. Difference between var, let, and const?
• var: function-scoped, can be redeclared.
• let: block-scoped, cannot be redeclared in the same scope.
• const: block-scoped, cannot be reassigned.
Q3. What are data types in JavaScript?
• Primitive: string, number, boolean, null, undefined, symbol, bigint.
• Non-primitive: objects, arrays, functions.
Q4. Difference between == and ===?
• ==: checks value after type conversion.
• ===: checks value and type (strict equality).
Q5. What is NaN?
It stands for "Not-a-Number". It's the result of invalid math operations like 0/0 or parseInt("abc").
2. Functions & Scope
Q6. What are arrow functions?
A shorter syntax for writing functions:
They don’t have their own this or arguments.
Q7. What is a callback function?
A function passed as an argument to another function, executed later.
Q8. Explain closures.
A closure is when a function "remembers" variables from its outer scope even after that scope has
finished executing.
Q9. Difference between function declaration and function expression?
• Declaration is hoisted:
• Expression is not hoisted:
Q10. What is IIFE (Immediately Invoked Function Expression)?
A function that runs immediately after it is defined.
3. Objects & Arrays
Q11. What is destructuring?
A way to unpack values:
Q12. Difference between for..in and for..of?
• for..in: iterates over keys of an object.
• for..of: iterates over values of an iterable (like arrays).
Q13. What is the spread operator?
Expands arrays/objects:
Q14. Difference between shallow copy and deep copy?
• Shallow copy: copies only references (e.g., [Link]).
• Deep copy: copies values recursively (e.g., structuredClone).
Q15. What are array methods like map, filter, reduce?
• map: transforms elements.
• filter: returns matching elements.
• reduce: reduces array to a single value.
4. Asynchronous JS
Q16. What is the event loop?
It handles async operations by pushing callbacks from the queue to the call stack.
Q17. Difference between synchronous and asynchronous code?
• Sync: runs line by line.
• Async: executes without blocking (e.g., setTimeout, fetch).
Q18. What is a promise?
An object representing the eventual result of an async operation. States: pending, fulfilled, rejected.
Q19. What are async and await?
Syntactic sugar for handling promises in a synchronous-looking way.
Q20. Difference between microtask and macrotask?
• Microtask: Promise callbacks.
• Macrotask: setTimeout, setInterval.
5. DOM & BOM
Q21. What is the DOM?
Document Object Model, a tree-like representation of an HTML document.
Q22. How to select elements in JS?
getElementById, querySelector, querySelectorAll.
Q23. Difference between innerHTML, innerText, and textContent?
• innerHTML: includes HTML.
• innerText: respects styling.
• textContent: plain text.
Q24. What is event bubbling and capturing?
• Bubbling: event moves from child to parent.
• Capturing: event moves from parent to child.
Q25. What is localStorage, sessionStorage, and cookies?
• localStorage: persistent, no expiry.
• sessionStorage: cleared on tab close.
• cookies: small data stored with expiry, sent with requests.
6. ES6+ Features
Q26. What are template literals?
String interpolation with backticks:
Q27. What is default parameter in functions?
Q28. What is a module in JS?
Code split into files using export and import.
Q29. Difference between nullish coalescing (??) and ||?
• ??: only checks null or undefined.
• ||: checks falsy values (0, '', false).
Q30. What are generators?
Functions that can pause and resume using yield.
7. OOP in JS
Q31. How to create a class in JS?
Q32. What is prototypal inheritance?
Objects inherit directly from other objects via prototypes.
Q33. Difference between class and function constructor?
• Class: modern syntax, cleaner.
• Constructor: older, function-based.
Q34. What is this in JavaScript?
Refers to the object that owns the function at execution.
Q35. Difference between call, apply, and bind?
• call: invoke with arguments.
• apply: invoke with array of arguments.
• bind: returns a new function with fixed this.
8. Advanced Topics
Q36. What is hoisting?
JavaScript moves declarations to the top before execution.
Q37. What is currying?
Transforming a function with multiple arguments into a sequence of single-argument functions.
Q38. Explain memoization.
Caching function results for faster future calls.
Q39. What is debouncing and throttling?
• Debounce: delay execution until user stops triggering.
• Throttle: limit execution to once per interval.
Q40. What are service workers?
Scripts running in the background enabling offline apps, caching, push notifications.
9. Error Handling & Security
Q41. How to handle errors in JS?
Using try...catch and .catch() in promises.
Q42. What are common security issues in JS?
XSS (Cross-site scripting), CSRF, code injection.
Q43. What is strict mode?
A restricted mode that makes code safer by avoiding silent errors.
Q44. What is CORS?
Cross-Origin Resource Sharing, controls resource access from different domains.
Q45. Difference between undefined and null?
• undefined: variable declared but not assigned.
• null: explicit empty value.
10. Performance & Best Practices
Q46. How to improve performance in JS?
• Minimize DOM manipulations.
• Use async/defer in scripts.
• Use memoization, caching.
Q47. What is tree-shaking?
Removing unused code during bundling.
Q48. Difference between deep freeze and shallow freeze in objects?
• [Link]: shallow freeze.
• For deep freeze, recursive freeze is required.
Q49. What are web workers?
Background threads for heavy computations without blocking UI.
Q50. How does garbage collection work in JS?
It automatically removes unused objects using reference counting and reachability checks.
Q51. What is the difference between [Link]() and [Link]()?
• preventDefault(): stops the default browser action (like form submit, link navigation).
• stopPropagation(): stops the event from bubbling up or capturing down the DOM.
Q52. What is the difference between map() and forEach()?
• map(): returns a new array with transformed elements.
• forEach(): executes a function for each element but doesn’t return anything.
Q53. What are WeakMap and WeakSet in JavaScript?
• WeakMap: stores key-value pairs where keys are objects and references are weak (garbage-
collected).
• WeakSet: stores objects only, also weakly referenced.