Skip to main content

JavaScript interview question and answer

1. What is JavaScript?

  • JavaScript is a high-level, interpreted programming language used to create interactive effects inside web browsers. It is also widely used in server-side development, mobile apps, and game development.

2. What is the difference between let, var, and const?

  • var is function-scoped, let and const are block-scoped. let allows reassignment, while const does not allow reassignment after initialization.

3. What is hoisting in JavaScript?

  • Hoisting is JavaScript's default behavior of moving declarations to the top of the current scope (global or function scope), which means variables and functions can be used before they are declared.

4. What are closures in JavaScript?

  • A closure is a function that has access to its own scope, the scope of the outer function, and the global scope even after the outer function has returned.

5. Explain the event loop in JavaScript.

  • The event loop is responsible for executing the code, collecting and processing events, and executing queued tasks (like handling asynchronous operations) in a non-blocking manner.

6. What are promises in JavaScript?

  • A promise is an object representing the eventual completion or failure of an asynchronous operation. It can be in one of three states: pending, fulfilled, or rejected.

7. What is the difference between synchronous and asynchronous code?

  • Synchronous code executes line by line, blocking subsequent code until the current one finishes. Asynchronous code allows other tasks to run while waiting for longer tasks (e.g., network requests) to complete.

8. What is this keyword in JavaScript?

  • The this keyword refers to the object it belongs to. Its value depends on the context in which it is used (e.g., inside a function, object, or class).

9. What is the difference between == and ===?

  • == compares values with type coercion, while === compares values and types without coercion.

10. What is the purpose of call(), apply(), and bind() methods?

  • These methods are used to change the value of this within a function. call() and apply() invoke a function immediately, while bind() creates a new function with the specified this value.

11. What is the difference between undefined and null?

  • undefined means a variable has been declared but has not yet been assigned a value. null is an assignment value that represents the intentional absence of any object value.

12. What is event bubbling?

  • Event bubbling is the process where an event starts from the innermost element (target) and propagates up to the outermost element (document).

13. What is an Immediately Invoked Function Expression (IIFE)?

  • An IIFE is a function that is executed immediately after its definition. It is often used to create a local scope to avoid polluting the global scope.

14. What are higher-order functions?

  • Higher-order functions are functions that take other functions as arguments or return functions as output.

15. What is the difference between localStorage and sessionStorage?

  • localStorage stores data with no expiration time, whereas sessionStorage stores data that is cleared when the page session ends (i.e., the browser is closed).

16. What are arrow functions, and how are they different from regular functions?

  • Arrow functions are a concise way to write functions. They do not have their own this or arguments object and inherit them from their parent scope.

17. What is the purpose of JSON.stringify() and JSON.parse()?

  • JSON.stringify() converts a JavaScript object into a JSON string, while JSON.parse() converts a JSON string back into a JavaScript object.

18. What is a prototype in JavaScript?

  • Every JavaScript object has a prototype. A prototype is also an object and is used as a blueprint for creating other objects. Properties and methods defined on the prototype can be accessed by all instances of the object.

19. What is the debounce function?

  • Debouncing is a technique to limit the rate at which a function is executed. The debounce function ensures that the function is executed only after a certain amount of time has passed since the last time it was invoked.

20. What is the difference between deep copy and shallow copy?

  • A shallow copy creates a new object that points to the same references as the original, while a deep copy creates a completely new object, copying all values and references recursively.

These are key JavaScript interview questions that test various concepts from basic to advanced levels