JavaScript Interview Questions & Answers

Top questions asked in interviews for freshers & experienced developers

1. What is JavaScript?
JavaScript is a scripting language used to create dynamic and interactive web pages.
2. Is JavaScript compiled or interpreted?
JavaScript is interpreted, but modern browsers use Just-In-Time compilation.
3. What are data types in JavaScript?
Primitive types: String, Number, Boolean, Null, Undefined, Symbol, BigInt. Non-primitive: Object.
4. Difference between var, let, and const?
var is function-scoped, let and const are block-scoped. const cannot be reassigned.
5. What is hoisting?
Hoisting moves variable and function declarations to the top of their scope.
6. What is scope in JavaScript?
Scope defines where variables can be accessed: global, function, or block scope.
7. What is closure?
A closure allows a function to access variables from its outer scope even after execution.
8. What is this keyword?
this refers to the object that is executing the current function.
9. What is arrow function?
Arrow functions provide a shorter syntax and do not have their own this.
10. What is callback function?
A callback is a function passed as an argument and executed later.
11. What is synchronous vs asynchronous?
Synchronous blocks execution, asynchronous allows non-blocking operations.
12. What is Promise?
A Promise represents a value that may be resolved or rejected in the future.
13. What are Promise states?
Pending, Fulfilled, and Rejected.
14. What is async/await?
It simplifies asynchronous code by making it look synchronous.
15. What is event loop?
The event loop handles asynchronous callbacks and execution order.
16. What is call stack?
It keeps track of function execution order.
17. What is DOM?
DOM represents the structure of an HTML document as objects.
18. Difference between getElementById and querySelector?
getElementById selects by ID, querySelector selects using CSS selectors.
19. What is event bubbling?
Events propagate from child to parent elements.
20. What is event capturing?
Events propagate from parent to child elements.
21. What is event delegation?
Handling events on a parent element instead of multiple child elements.
22. What is preventDefault()?
It prevents default browser behavior.
23. What is stopPropagation()?
It stops event bubbling or capturing.
24. What is strict mode?
'use strict' enforces stricter parsing and error handling.
25. What is JSON?
JSON is a lightweight data format for storing and transferring data.
26. Difference between == and ===?
== compares values, === compares value and type.
27. What is NaN?
NaN means Not-a-Number and represents invalid numeric operations.
28. What is type coercion?
Automatic conversion of data types during operations.
29. What is truthy and falsy?
Values that evaluate to true or false in boolean context.
30. What is array?
An array stores multiple values in a single variable.
31. Difference between map, filter, and reduce?
map transforms, filter selects, reduce accumulates values.
32. What is spread operator?
It expands iterable elements.
33. What is rest parameter?
It collects remaining arguments into an array.
34. What is destructuring?
It extracts values from arrays or objects.
35. What is prototype?
It allows objects to inherit properties in JavaScript.
36. What is prototypal inheritance?
Objects inherit properties directly from other objects.
37. What is class in JavaScript?
A blueprint for creating objects using constructor and methods.
38. What is constructor?
A function used to initialize object properties.
39. What is instanceof?
It checks whether an object belongs to a constructor.
40. What is localStorage?
Browser storage with no expiration time.
41. What is sessionStorage?
Stores data for the duration of the session.
42. Difference between localStorage and cookies?
localStorage stores more data and is client-only, cookies are sent to server.
43. What is fetch API?
It is used to make HTTP requests.
44. What is CORS?
It is a security feature that controls cross-origin requests.
45. What is debounce?
It limits function execution after a delay.
46. What is throttle?
It limits function execution to a fixed rate.
47. What is memory leak?
Unused memory that is not released.
48. What is garbage collection?
Automatic memory management in JavaScript.
49. What is Web API?
Browser-provided APIs like DOM, Fetch, and Timers.
50. What is setTimeout?
Executes a function after a delay.
51. What is setInterval?
Executes a function repeatedly at intervals.
52. What is clearTimeout?
Cancels a timeout.
53. What is clearInterval?
Cancels an interval.
54. What is module?
A reusable piece of code with import and export.
55. What is ES6?
A major update introducing modern JavaScript features.
56. What is default parameter?
A parameter with a default value.
57. What is optional chaining?
It safely accesses nested object properties.
58. What is nullish coalescing?
It returns right-hand value if left is null or undefined.
59. What is Service Worker?
It enables offline support and background tasks.
60. What is Progressive Web App (PWA)?
A web app that behaves like a native application.