Javascript Interview Questions with Answers

Syed Khizaruddin
Frontend Weekly
Published in
16 min readNov 16, 2023

100 questions with answers.

1. What is JavaScript?

Answer: JavaScript is a high-level, interpreted programming language primarily used for creating interactive and dynamic web pages.

2. What is the DOM in the context of JavaScript?

Answer: The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a document as a tree of objects, allowing developers to manipulate the content and structure of a web page dynamically.

3. Explain the difference between let, var, and const in JavaScript.

Answer: var is function-scoped, let is block-scoped, and const is also block-scoped but its value cannot be re-assigned after declaration.

4. What is the purpose of closures in JavaScript?

Answer: Closures allow functions to access variables from their outer scope even after the outer function has finished executing. They help create private variables and methods.

5. What is the event loop in JavaScript?

Answer: The event loop is a mechanism that allows asynchronous JavaScript code to execute in a non-blocking way. It continuously checks the message queue and executes tasks in a loop.

6. What is the difference between == and === in JavaScript?

Answer: == is equality operator that performs type coercion, while === is the strict equality operator that checks both value and type.

7. Explain the concept of hoisting in JavaScript.

Answer: Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase.

8. How does this keyword work in JavaScript?

Answer: The value of this depends on how a function is called. In the global scope, it refers to the global object. In a method, it refers to the object on which the method is called.

9. What is a callback function in JavaScript?

Answer: callback function is a function passed as an argument to another function, which will be invoked later when a specific event occurs or a condition is met.

10. Explain the purpose of the bind method in JavaScript.

Answer: The bind method is used to create a new function that, when called, has its this keyword set to a specific value, allowing you to explicitly specify the value of this for a function.

11. What is the difference between null and undefined in JavaScript?

Answer: null is an assignment value representing the absence of any object value, while undefined is a primitive value automatically assigned to variables that have been declared but not initialized.

12. How does prototypal inheritance work in JavaScript?

Answer: JavaScript uses prototypal inheritance, where objects can inherit properties and methods from other objects. Each object has a prototype object, and if a property or method is not found on the object itself, JavaScript looks for it in the prototype chain.

13. Explain the concept of debouncing in JavaScript.

Answer: Debouncing is a technique used to ensure that time-consuming tasks do not fire so often, making them more efficient. It involves delaying the execution of a function until after a certain period of inactivity.

14. What is the purpose of the apply and call methods in JavaScript?

Answer: Both apply and call are used to invoke a function with a specified this value. The difference lies in how arguments are passed: apply takes an array of arguments, while call takes the arguments individually.

15. Explain the concept of event delegation in JavaScript.

Answer: Event delegation involves assigning a single event listener to a common ancestor of multiple elements. This way, you can handle events for all these elements in one place, improving performance.

16. What is the purpose of the Promise object in JavaScript?

Answer: A Promise is an object representing the eventual completion or failure of an asynchronous operation. It provides a cleaner way to handle asynchronous code compared to callbacks.

17. What is the difference between let and const in JavaScript regarding variable reassignment?

Answer: let allows variable reassignment, while const does not. Once a value is assigned to a const variable, it cannot be changed.

18. How does the async/await feature in JavaScript simplify working with asynchronous code?

Answer: async/await is a syntactic sugar on top of promises, making asynchronous code look and behave more like synchronous code. It helps in writing asynchronous code that is easier to read and understand.

19. What is the purpose of the localStorage and sessionStorage objects in JavaScript?

Answer: localStorage and sessionStorage are web storage objects that allow you to store key-value pairs locally in a user’s browser. The main difference is that data stored in localStorage persists even after the browser is closed, while data in sessionStorage is cleared when the session ends.

20. How can you clone an object in JavaScript?

Answer: To clone an object in JavaScript, you can use methods like Object.assign(), the spread operator (…), or JSON.parse() and JSON.stringify().

Here’s an example using the spread operator:

const originalObject = { key1: 'value1', key2: 'value2' };
const clonedObject = { ...originalObject };

21. Explain the concept of the same-origin policy in JavaScript.

Answer: The same-origin policy is a security measure that restricts web pages from making requests to a different domain than the one that served the web page. This policy helps prevent malicious scripts from accessing sensitive data.

22. What is the purpose of the map function in JavaScript?

Answer: The map function is used to create a new array by applying a provided function to each element of an existing array, without mutating the original array.

23. What is a closure, and how is it used in JavaScript?

Answer: A closure is a combination of a function and the lexical environment within which that function was declared. Closures allow a function to access variables from its outer scope even after the outer function has finished executing.

24. How does JavaScript handle asynchronous operations?

Answer: JavaScript uses mechanisms such as callbacks, promises, and async/await to handle asynchronous operations. These help manage the flow of asynchronous code and improve readability.

25. Explain the concept of the prototype chain in JavaScript.

Answer: The prototype chain is a mechanism by which objects in JavaScript inherit properties and methods from their prototypes. If a property or method is not found on the object itself, JavaScript looks for it in the object’s prototype, and so on, forming a chain.

26. What is the purpose of the splice method in JavaScript?

Answer: The splice method is used to add or remove elements from an array. It can both add new elements at a specified index and remove elements from a specific index.

27. How does event bubbling differ from event capturing in JavaScript?

Answer: Event bubbling and event capturing are two phases of event propagation in the DOM. In event bubbling, the innermost element’s event is handled first, and then the event bubbles up to the outer elements. Event capturing involves handling the event at the outermost element first and then capturing it as it goes down to the inner elements.

28. What is the purpose of the filter function in JavaScript?

Answer: The filter function is used to create a new array containing only the elements that pass a provided test. It does not mutate the original array.

29. Explain the concept of the try…catch statement in JavaScript.

Answer: The try…catch statement is used for handling exceptions (errors) in JavaScript. Code that might throw an exception is placed in the try block, and if an exception occurs, it is caught and handled in the catch block.

30. What is the difference between null and undefined in JavaScript?

Answer: null is an assignment value representing the absence of any object value, while undefined is a primitive value automatically assigned to variables that have been declared but not initialized.

31. What is the purpose of the setTimeout function in JavaScript?

Answer: The setTimeout function is used to delay the execution of a function or code block by a specified amount of time (in milliseconds).

32. Explain the concept of the EventEmitter in Node.js.

Answer: EventEmitter is a class in Node.js that facilitates communication between objects in a publish/subscribe pattern. It allows one object to notify a set of listeners when an event occurs.

33. What is the purpose of the Object.keys() method in JavaScript?

Answer: Object.keys() is used to return an array containing the names of all the enumerable properties of an object.

34. How does the for…of loop differ from the for…in loop in JavaScript?

Answer: The for…of loop is used to iterate over the values of an iterable object (arrays, strings, etc.), while the for…in loop is used to iterate over the properties of an object.

35. Explain the concept of the fetch API in JavaScript.

Answer: The fetch API is a modern way to make HTTP requests in JavaScript. It returns a Promise that resolves to the Response to that request, allowing for more flexible and powerful handling of network requests.

36. What is the purpose of the reduce function in JavaScript?

Answer: The reduce function is used to reduce an array to a single value by applying a specified function to each element and accumulating the result.

37. How does the localStorage differ from sessionStorage in JavaScript?

Answer: Both localStorage and sessionStorage are web storage objects, but localStorage persists even after the browser is closed, while sessionStorage is cleared when the session ends.

38. Explain the concept of currying in JavaScript.

Answer: Currying is a technique in functional programming where a function is transformed into a sequence of functions, each taking a single argument. It allows for partial application of functions.

39. What is the purpose of the Object.create() method in JavaScript

Answer: Object.create() is used to create a new object with the specified prototype object and properties. It provides a way to implement inheritance in JavaScript.

40. How does the Promise.all method work in JavaScript?

Answer: Promise.all is used to wait for all the promises in an iterable to resolve or reject. It returns a single promise that resolves with an array of the resolved values.

41. Explain the concept of the typeof operator in JavaScript.

Answer: The typeof operator is used to determine the type of a variable or expression. It returns a string representing the data type (e.g., “number,” “string,” “object,” etc.).

42. What is the purpose of the Array.isArray() method in JavaScript?

Answer: Array.isArray() is used to check if a value is an array. It returns true if the value is an array and false otherwise.

43. How does the addEventListener method work in JavaScript?

Answer: addEventListener is used to attach an event handler to an element. It takes the event type and the function to be executed when the event occurs.

44. Explain the concept of the WeakMap in JavaScript.

Answer: WeakMap is a collection of key-value pairs where the keys must be objects. Unlike a Map, a WeakMap allows for garbage collection of its keys, making it suitable for scenarios where references to keys should not prevent them from being collected.

45. What is the purpose of the Number object in JavaScript?

Answer: The Number object is a wrapper object that allows you to work with numeric values. It provides methods for converting values to numbers and performing mathematical operations.

46. How does JavaScript handle variable hoisting?

Answer: Variable and function declarations are hoisted to the top of their containing scope during the compilation phase, allowing them to be used before they are declared in the code.

47. What is the purpose of the Array.from() method in JavaScript?

Answer: Array.from() is used to create a new array instance from an array-like or iterable object. It provides a way to convert objects that are not arrays into arrays.

48. Explain the concept of memoization in JavaScript.

Answer: Memoization is a technique used to optimize function calls by caching the results of expensive function calls and returning the cached result when the same inputs occur again.

49. What is the purpose of the Object.freeze() method in JavaScript?

Answer: Object.freeze() is used to freeze an object, making it immutable. Once an object is frozen, its properties cannot be added, modified, or removed.

50. How does the this keyword work in arrow functions in JavaScript?

Answer: In arrow functions, the value of this is lexically scoped, meaning it is determined by the surrounding scope and does not change with the function’s execution context. Arrow functions do not have their own this context.

51. What is the purpose of the Object.getOwnPropertyDescriptor() method in JavaScript?

Answer: Object.getOwnPropertyDescriptor() is used to get the descriptor for a property of an object. The descriptor contains information such as whether the property is writable, enumerable, and configurable.

52. Explain the concept of the Event Loop in the context of JavaScript concurrency.

Answer: The Event Loop is a crucial part of JavaScript’s concurrency model. It continuously checks the call stack and message queue, ensuring that asynchronous operations are executed in a non-blocking manner.

53. How can you handle errors in asynchronous code in JavaScript?

Answer: You can handle errors in asynchronous code using try…catch blocks or by attaching a .catch() handler to a promise.

54. What is the purpose of the Array.map() method, and how does it differ from Array.forEach()?

Answer: Array.map() creates a new array with the results of calling a provided function on every element, while Array.forEach() executes a provided function once for each array element but does not create a new array.

55. Explain the concept of the Symbol data type in JavaScript.

Answer: Symbol is a primitive data type introduced in ECMAScript 6. It is used to create unique identifiers for object properties and avoids naming conflicts.

56. What is the purpose of the Object.is() method in JavaScript?

Answer: Object.is() is used to compare two values for equality. It differs from === in handling special values like NaN and -0 more consistently.

57. How does the rest parameter work in JavaScript functions?

Answer: The rest parameter allows a function to accept an indefinite number of arguments as an array, using the syntax …variableName. It collects remaining arguments into an array.

58. What is the difference between window and document in the context of the browser environment?

Answer: window represents the global browser window and contains properties like document. document represents the HTML document loaded in the window and provides methods to manipulate its content.

59. Explain the concept of the WeakSet in JavaScript.

Answer: WeakSet is a collection of objects where the objects must be unique. Similar to WeakMap, it allows for garbage collection of its elements.

60. What is a closure, and how can it lead to memory leaks in JavaScript?

Answer: A closure is the combination of a function and the lexical environment within which that function was declared. Memory leaks can occur if closures create references to variables that are no longer needed, preventing them from being garbage collected.

61. How does the Array.reduce() method work, and what is an accumulator function?

Answer: Array.reduce() is used to reduce an array to a single value by applying an accumulator function to each element. The accumulator function takes four parameters: accumulator, current value, current index, and the array.

62. Explain the purpose of the async keyword in JavaScript function declarations.

Answer: The async keyword is used to define asynchronous functions, which return promises. It enables the use of await within the function, allowing better handling of asynchronous code.

63. What is the purpose of the RegExp object in JavaScript?

Answer: The RegExp object is used for working with regular expressions. It provides methods for matching text patterns within strings.

64. How does the Array.filter() method work in JavaScript?

Answer: Array.filter() creates a new array with all elements that pass a provided function’s test. The function is called once for each element in the array.

65. What is the purpose of the Promise.race() method in JavaScript

Answer: Promise.race() is used to race multiple promises against each other, returning the value (or error) of the first promise that resolves or rejects.

66. Explain the concept of function currying and how it is achieved in JavaScript.

Answer: Function currying is the process of transforming a function with multiple arguments into a series of functions with a single argument. This can be achieved by returning a series of functions, each taking one argument, from the original function.

67. What is the purpose of the encodeURIComponent() function in JavaScript?

Answer: encodeURIComponent() is used to encode special characters in a URL, ensuring that they are properly represented in the encoded URL.

68. How does the Object.values() method work in JavaScript?

Answer: Object.values() returns an array of the enumerable property values of an object, in the same order as provided by a for…in loop.

69. Explain the concept of the Proxy object in JavaScript.

Answer: Proxy is an object that wraps another object and intercepts operations, allowing custom behavior. It is often used for creating middleware, validation, or tracking changes.

70. What is the purpose of the Array.slice() method in JavaScript?

Answer: Array.slice() is used to extract a portion of an array into a new array without modifying the original array. It takes start and end indices as arguments.

71. Explain the concept of the Web Workers in JavaScript.

Answer: Web Workers allow for parallel execution of scripts in the background, separate from the main execution thread. They are useful for handling computationally intensive tasks without affecting the responsiveness of the user interface.

72. What is the purpose of the Array.some() method in JavaScript?

Answer: Array.some() tests whether at least one element in the array passes the provided function’s test. It returns a boolean value.

73. How does the try…finally statement differ from try…catch in JavaScript?

Answer: try…finally is used to specify a block of code that will be executed regardless of whether an exception is thrown or caught. It is often used for cleanup operations.

74. What is the Event Loop in JavaScript, and how does it contribute to the language’s asynchronous nature?

Answer: The Event Loop is a continuous process that handles the execution of code, including asynchronous operations, in a non-blocking way. It allows JavaScript to efficiently manage asynchronous tasks and callbacks.

75. Explain the purpose of the Array.unshift() method in JavaScript.

Answer: Array.unshift() is used to add one or more elements to the beginning of an array. It modifies the original array and returns the new length.

76. What is the super keyword used for in JavaScript classes?

Answer: In a class, super is used to call the parent class’s constructor. It is also used to access the parent class’s methods.

77. How does the Object.setPrototypeOf() method work in JavaScript?

Answer: Object.setPrototypeOf() is used to set the prototype of an object. It takes two arguments: the object to be modified and the object’s new prototype.

78. What is the purpose of the Array.concat() method in JavaScript?

Answer: Array.concat() is used to merge two or more arrays. It creates a new array without modifying the existing arrays.

79. Explain the concept of the event.preventDefault() method in JavaScript.

Answer: event.preventDefault() is used to stop the default action of an event. It is commonly used in event handlers to prevent a form from submitting or a link from navigating to a new page.

80. How does the Array.every() method work in JavaScript?

Answer: Array.every() tests whether all elements in the array pass the provided function’s test. It returns a boolean value.

81. What is the purpose of the Object.isExtensible() method in JavaScript?

Answer: Object.isExtensible() is used to check if an object is extensible, meaning if new properties can be added to it. It returns a boolean value.

82. Explain the concept of the Promise.finally() method in JavaScript.

Answer: Promise.finally() is used to specify a function that will be called when a promise is settled, whether it is resolved or rejected. It is often used for cleanup operations.

83. What is the arguments object in JavaScript and how is it used?

Answer: The arguments object is an array-like object available inside all functions that contains the values of the arguments passed to that function. It allows you to access all the arguments dynamically.

84. How does the String.trim() method work in JavaScript?

Answer: String.trim() is used to remove whitespace from both ends of a string. It does not modify the original string; instead, it returns a new string with the leading and trailing whitespace removed.

85. Explain the purpose of the Object.seal() method in JavaScript.

Answer: Object.seal() is used to prevent new properties from being added to an object and prevents existing properties from being removed. However, it does not affect the property values.

86. What is the purpose of the Map object in JavaScript?

Answer: Map is a collection of key-value pairs where the keys can be any data type. It allows for an efficient way to store and retrieve data.

87. How does the Array.findIndex() method work in JavaScript?

Answer: Array.findIndex() is used to find the index of the first element in an array that satisfies a provided testing function. It returns -1 if no such element is found.

88. Explain the concept of the Blob object in JavaScript.

Answer: Blob represents a binary large object, allowing the creation of objects containing data such as images or other binary data.

89. What is the purpose of the instanceof operator in JavaScript?

Answer: instanceof is used to check if an object is an instance of a particular class or constructor function. It returns a boolean value.

90. How does the JSON.stringify() method work in JavaScript?

Answer: JSON.stringify() is used to convert a JavaScript object or value to a JSON string. It can also accept a replacer function or an array to customize the stringification process.

91. What is the purpose of the Object.create() method in JavaScript?

Answer: Object.create() is used to create a new object with the specified prototype object and properties. It provides a way to implement inheritance in JavaScript.

92. How does the String.replace() method work in JavaScript?

Answer: String.replace() is used to replace a specified substring or pattern with another substring. It can take a regular expression or a string as the search pattern.

93. Explain the concept of memoization in JavaScript.

Answer: Memoization is a technique used to optimize function calls by caching the results of expensive function calls and returning the cached result when the same inputs occur again.

94. What is the purpose of the Object.isFrozen() method in JavaScript?

Answer: Object.isFrozen() is used to check if an object is frozen, meaning if its properties cannot be added, modified, or removed. It returns a boolean value.

95. How does the Array.splice() method work in JavaScript?

Answer: Array.splice() is used to add or remove elements from an array at a specified index. It can both add new elements and remove existing elements.

96. Explain the purpose of the Object.defineProperty() method in JavaScript.

Answer: Object.defineProperty() is used to define or modify a property on an object with a specified descriptor, controlling various aspects such as whether the property is writable, enumerable, and configurable.

97. What is the purpose of the Array.reverse() method in JavaScript?

Answer: Array.reverse() is used to reverse the order of elements in an array. It modifies the original array and returns a reference to the reversed array.

98. How does the parseInt() function work in JavaScript, and what is the radix parameter?

Answer: parseInt() is used to parse a string and convert it to an integer. The radix parameter specifies the base of the numeral system and is optional.

99. Explain the concept of the Promise.allSettled() method in JavaScript.

Answer: Promise.allSettled() is used to wait for all the promises in an iterable to settle (either resolved or rejected) and returns an array of objects representing the outcome of each promise.

100. What is the purpose of the Object.keys() method in JavaScript?

Answer: Object.keys() is used to return an array containing the names of all the enumerable properties of an object.

--

--