JavaScript Interview Questions and Answers
1. What is JavaScript?
JavaScript is a high-level, interpreted programming language used to make web pages interactive.
It runs in the browser and can also be used on servers via [Link].
2. Difference between var, let, and const?
`var` is function-scoped, while `let` and `const` are block-scoped. `const` cannot be reassigned,
`let` can.
3. What are JavaScript data types?
Primitive types include string, number, boolean, null, undefined, symbol, bigint; non-primitive is
object.
4. What is an array?
An ordered collection of elements accessed by index, e.g., `[1, 2, 3]`.
5. What is an object?
An unordered collection of key-value pairs. Example: `{name: 'John', age: 30}`.
6. Explain function declaration vs expression.
Declaration is hoisted (`function test(){}`), expression is not (`const test = function(){}`); both define
functions.
7. What are arrow functions?
A shorter syntax for functions, e.g. `const add = (a,b) => a+b;`. They don’t bind their own `this`.
8. What is scope in JavaScript?
Scope defines variable accessibility — global, function, and block scope.
9. Explain hoisting.
JavaScript moves variable and function declarations to the top of their scope before execution.
10. What is a closure?
A closure allows a function to access variables from its parent scope even after the parent has
closed.
11. What is the DOM?
Document Object Model — a tree representation of the webpage that JavaScript can manipulate
dynamically.
12. How to select elements in DOM?
Using `getElementById`, `getElementsByClassName`, `querySelector`, `querySelectorAll`, etc.
13. What is addEventListener?
It attaches an event handler to an element, e.g., `[Link]('click', fn)`.
14. Explain callback functions.
A callback is a function passed as an argument to another function to be executed later.
15. What are Promises?
Promises handle async operations — states: pending, fulfilled, rejected. Example: `fetch()` returns
a Promise.
16. Explain async/await.
Async/await provides a cleaner way to write asynchronous code using Promises.
17. What is the fetch API?
A modern interface to make HTTP requests that returns a Promise.
18. Explain synchronous vs asynchronous.
Synchronous executes sequentially; asynchronous allows non-blocking operations via callbacks,
promises, etc.
19. What is the 'this' keyword?
`this` refers to the object that owns the current executing code. It depends on how the function is
called.
20. What are template literals?
String literals using backticks allowing embedded expressions, e.g., `` `Hello ${name}` ``.
21. What is destructuring?
Extracting values from arrays or objects into variables, e.g., `const {name} = person;`.
22. What is the spread operator?
Expands elements, e.g., `[...arr1, ...arr2]` or `{...obj}`.
23. What is event bubbling and capturing?
Bubbling: event flows from child to parent; capturing: parent to child.
24. What is a higher-order function?
A function that takes another function as argument or returns one.
25. What are map, filter, and reduce?
`map()` transforms, `filter()` filters, and `reduce()` aggregates array data.
26. What is the event loop?
Mechanism that handles asynchronous callbacks via the call stack and message queue.
27. What is a Promise chain?
A series of `.then()` calls on a Promise to handle async results sequentially.
28. What are microtasks and macrotasks?
Microtasks: Promises; Macrotasks: setTimeout, setInterval — queued differently in event loop.
29. How do you handle errors?
Using try/catch/finally blocks or `.catch()` for Promises.
30. What is prototype inheritance?
Objects inherit from other objects via their prototype chain.
31. Difference between class and prototype?
Classes are syntactic sugar over prototypes — same inheritance model.
32. What is a module in JS?
Reusable code file. Use `export` and `import` for modularization.
33. Explain debouncing and throttling.
Debounce delays function execution; throttle limits function execution rate.
34. How to prevent default browser behavior?
Use `[Link]()`.
35. What is JSON?
JavaScript Object Notation — lightweight format for data exchange.
36. What is localStorage?
Stores data in browser persistently. Access via `[Link]/getItem`.
37. What is sessionStorage?
Stores data temporarily per browser tab session.
38. What are cookies?
Small pieces of data stored by browsers, often for sessions or tracking.
39. How to optimize JS performance?
Use caching, debounce events, minimize DOM reflows, use async loading.
40. Explain ES6 features.
Includes `let/const`, arrow functions, destructuring, spread/rest, modules, promises, classes, etc.