Here are 101 JavaScript interview questions with answers:
1. What is JavaScript? JavaScript is a high-level, interpreted programming language used primarily for creating interactive web pages.
2. What are the different data types in JavaScript? The data types in JavaScript include string, number, boolean, object, undefined, and null.
3. What is the difference between null
and undefined
? null
represents the intentional absence of any object value, while undefined
indicates the absence of a value or an uninitialized variable.
4. How do you declare variables in JavaScript? Variables in JavaScript can be declared using the var
, let
, or const
keywords.
5. What is hoisting in JavaScript? Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their respective scopes during the compilation phase.
6. What is the purpose of the this
keyword in JavaScript? The this
keyword refers to the object that the function is executing in. Its value depends on how a function is called.
7. What is the difference between ==
and ===
operators? The ==
operator compares values, allowing type coercion, while the ===
operator compares both values and types strictly.
8. Explain event delegation in JavaScript. Event delegation is a technique where a parent element handles events triggered by its child elements. It helps optimize performance and reduce event listeners.
9. What is a closure in JavaScript? A closure is a function that retains access to variables from its outer scope even after the outer function has finished executing.
10. How do you create an object in JavaScript? Objects in JavaScript can be created using object literals, constructor functions, or the class
syntax introduced in ECMAScript 6.
11. What is the purpose of the bind()
method in JavaScript? The bind()
method creates a new function that, when called, has a specified this
value and arguments passed to it.
12. What are the different ways to loop through an array in JavaScript? You can loop through an array using for
loops, forEach()
, map()
, filter()
, reduce()
, and other array methods.
13. How do you handle errors in JavaScript? Errors in JavaScript can be handled using try-catch blocks to catch and handle exceptions.
14. What is the difference between synchronous and asynchronous programming in JavaScript? Synchronous programming executes tasks in a sequential order, while asynchronous programming allows tasks to run concurrently and handles callbacks or promises.
15. How does prototypal inheritance work in JavaScript? JavaScript uses prototypal inheritance, where objects can inherit properties and methods from other objects via prototype chains.
16. What are arrow functions in JavaScript? Arrow functions are a concise syntax for writing function expressions in JavaScript, with a lexically bound this
value.
17. Explain the concept of event bubbling in JavaScript. Event bubbling is the process where an event triggered on a nested element propagates through its parent elements in the DOM hierarchy.
18. What is the purpose of the setTimeout()
function in JavaScript? The setTimeout()
function is used to execute a specified function after a certain delay, measured in milliseconds.
19. How do you handle asynchronous operations in JavaScript? Asynchronous operations in JavaScript can be handled using callbacks, promises, or the newer async/await
syntax introduced in ECMAScript 2017.
20. What is the purpose of the localStorage
object in JavaScript? The localStorage
object allows you to store key-value pairs in the browser’s storage, persisting even after the browser is closed.
21. What is event propagation in JavaScript? Event propagation refers to the process of an event being handled by multiple elements in the DOM hierarchy, either through capturing or bubbling phases.
22. How do you handle exceptions in JavaScript? Exceptions in JavaScript can be handled using try-catch-finally blocks, where code that might throw an exception is placed inside the try block, and the catch block handles any thrown exceptions.
23. Explain the concept of “hoisting” in JavaScript. Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their respective scopes during the compilation phase, allowing you to use them before they are declared.
24. What is the difference between null
and undefined
in JavaScript? null
represents an intentional absence of any object value, while undefined
indicates the absence of a value or an uninitialized variable.
25. What is a callback function in JavaScript? A callback function is a function that is passed as an argument to another function and gets executed at a later time or after a particular event occurs.
26. What is the purpose of the JSON.parse()
method in JavaScript? The JSON.parse()
method is used to parse a JSON string and convert it into a JavaScript object.
27. How do you check if a variable is an array in JavaScript? You can check if a variable is an array using the Array.isArray()
method, which returns true
if the variable is an array and false
otherwise.
28. What is the purpose of the filter()
method in JavaScript? The filter()
method creates a new array with all elements that pass the provided test implemented by a callback function.
29. Explain the concept of currying in JavaScript. Currying is a technique in functional programming where a function with multiple arguments is transformed into a sequence of functions, each taking a single argument.
30. What is the difference between null
and undefined
in JavaScript? null
is an assigned value that indicates the intentional absence of any object value, while undefined
is a variable that has been declared but not assigned a value.
31. How does JavaScript handle inheritance? JavaScript uses prototypal inheritance, where objects can inherit properties and methods from other objects through prototype chains.
32. What is the purpose of the map()
method in JavaScript? The map()
method creates a new array populated with the results of calling a provided function on every element in the calling array.
33. What is the difference between let
, const
, and var
in JavaScript? let
and const
are block-scoped variables introduced in ECMAScript 6, while var
is function-scoped. let
allows reassignment, while const
is a constant value that cannot be reassigned.
34. Explain the concept of event delegation in JavaScript. Event delegation is a technique where you attach a single event listener to a parent element to handle events triggered by its child elements. It helps optimize performance and reduce memory consumption.
35. How do you convert a string to lowercase in JavaScript? You can convert a string to lowercase using the toLowerCase()
method.
36. What is a closure in JavaScript and why is it useful? A closure is a combination of a function and the lexical environment within which that function was declared. It allows the function to retain access to variables from its outer scope, even after the outer function has finished executing.
37. What is the purpose of the reduce()
method in JavaScript? The reduce()
method applies a function against an accumulator and each element in an array, resulting in a single value.
38. How do you deep copy an object in JavaScript? There are multiple ways to deep copy an object in JavaScript, including using JSON.parse(JSON.stringify(obj))
, the spread operator ({...obj}
), or libraries like Lodash’s cloneDeep()
.
39. What is the purpose of the find()
method in JavaScript? The find()
method returns the first element in an array that satisfies a provided testing function.
40. How do you reverse a string in JavaScript? You can reverse a string by converting it to an array using split()
, then reversing the array using reverse()
, and finally joining the elements back using join()
.
41. What is the difference between let
and var
in JavaScript? let
is block-scoped and has a limited scope to the block it is defined in, while var
is function-scoped and has a broader scope within the function.
42. How do you check if an object has a specific property in JavaScript? You can use the hasOwnProperty()
method to check if an object has a specific property.
43. Explain the concept of event capturing and event bubbling in JavaScript. Event capturing and event bubbling are two different phases of event propagation in the DOM. During the capturing phase, the event is first captured by the outermost ancestor element, and during the bubbling phase, it is propagated upwards from the target element.
44. What is the purpose of the some()
method in JavaScript? The some()
method tests whether at least one element in an array satisfies a provided testing function.
45. What is a callback function in JavaScript? Give an example. A callback function is a function that is passed as an argument to another function and is invoked inside that function. An example is the setTimeout()
function where you can pass a callback function to be executed after a certain delay.
46. How do you handle asynchronous programming in JavaScript? Asynchronous programming in JavaScript can be handled using callbacks, promises, or async/await syntax, allowing non-blocking execution of code and handling of asynchronous tasks.
47. What is the purpose of the slice()
method in JavaScript? The slice()
method returns a shallow copy of a portion of an array into a new array object, allowing you to extract a specific range of elements.
48. How do you clone an array in JavaScript? You can clone an array using the slice()
method, spread operator, or Array.from()
method.
49. Explain the concept of event.preventDefault() in JavaScript. The event.preventDefault()
method is used to stop the default behavior of an event from occurring, such as preventing a form submission or a link from redirecting.
50. What is the purpose of the push()
method in JavaScript? The push()
method adds one or more elements to the end of an array and returns the new length of the array.
51. How do you check if a variable is of a specific type in JavaScript? You can use the typeof
operator to check the type of a variable, or the instanceof
operator to check if an object is an instance of a specific class.
52. Explain the concept of lexical scoping in JavaScript. Lexical scoping means that the scope of a variable is determined by its position within the source code, and nested functions have access to variables defined in their outer function.
53. What is the purpose of the Object.keys()
method in JavaScript? The Object.keys()
method returns an array of a given object’s own enumerable property names.
54. How do you remove an element from an array in JavaScript? You can use methods like splice()
, pop()
, or shift()
to remove elements from an array.
55. What is the difference between a shallow copy and a deep copy in JavaScript? A shallow copy of an object or array creates a new reference to the original object, while a deep copy creates a completely independent copy of the object with all nested properties and values.
56. Explain the concept of lexical this in JavaScript. Lexical this
refers to the value of this
in a function that is defined using arrow function syntax, which binds this
to the surrounding lexical context.
57. How do you compare two objects for equality in JavaScript? JavaScript does not have a built-in method to compare two objects for equality. You would need to manually compare their properties and values.
58. What is the purpose of the toUpperCase()
method in JavaScript? The toUpperCase()
method converts a string to uppercase letters.
59. How do you convert a string to an integer in JavaScript? You can use the parseInt()
or Number()
functions to convert a string to an integer.
60. What are the differences between ==
and ===
in JavaScript? The ==
operator performs type coercion and checks for equality, while ===
strictly checks for equality without type coercion.
61. What is hoisting in JavaScript? Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their scope during the compilation phase.
62. Explain the concept of closures in JavaScript. Closures are functions that have access to variables from their outer lexical environment, even after the outer function has finished executing.
63. What is the purpose of the map()
method in JavaScript? The map()
method creates a new array by applying a provided function to each element of the original array.
64. How do you remove duplicates from an array in JavaScript? You can remove duplicates from an array using various methods like using Set
, filter()
, or reduce()
.
65. What is the difference between null
and undefined
in JavaScript? null
represents the intentional absence of any object value, while undefined
represents the absence of an assigned value.
66. How do you check if a variable is an array in JavaScript? You can use the Array.isArray()
method to check if a variable is an array.
67. Explain the concept of event delegation in JavaScript. Event delegation is a technique where you attach an event listener to a parent element and listen for events that occur on its child elements. This is useful when dynamically adding or removing elements.
68. What is the purpose of the reduce()
method in JavaScript? The reduce()
method applies a function to an accumulator and each element in an array, reducing it to a single value.
69. How do you check if a variable is empty in JavaScript? You can check if a variable is empty by comparing it to null
, undefined
, or an empty string.
70. What are the different types of error handling in JavaScript? Error handling in JavaScript can be done using try...catch
blocks, throwing custom errors, or using promises and catch()
method.
71. What is the purpose of the filter()
method in JavaScript? The filter()
method creates a new array with all elements that pass the provided testing function.
72. How do you convert an object to a JSON string in JavaScript? You can use the JSON.stringify()
method to convert an object to a JSON string.
73. Explain the concept of event propagation in JavaScript. Event propagation is the process of an event being handled by multiple elements in the DOM tree, either through event capturing or event bubbling.
74. What is the purpose of the concat()
method in JavaScript? The concat()
method is used to merge two or more arrays, creating a new array that contains the concatenated elements.
75. How do you sort an array in JavaScript? You can use the sort()
method to sort an array alphabetically or numerically. You can also provide a custom comparison function for specific sorting criteria.
76. What is the difference between synchronous and asynchronous code in JavaScript? Synchronous code is executed sequentially, blocking further execution until the current task is completed, while asynchronous code allows multiple tasks to be executed concurrently without blocking.
77. How do you convert a string to a date object in JavaScript? You can use the Date()
constructor or the new Date()
method to convert a string to a date object.
78. What is the purpose of the forEach()
method in JavaScript? The forEach()
method executes a provided function once for each element in an array.
79. How do you check if an element exists in an array in JavaScript? You can use the includes()
method to check if an element exists in an array.
80. What is the difference between let
, const
, and var
in JavaScript? let
and const
are block-scoped variables, while var
is function-scoped. let
allows reassignment, const
is a constant that cannot be reassigned, and var
can be reassigned and hoisted.
81. What is the difference between slice()
and splice()
methods in JavaScript? The slice()
method returns a shallow copy of an array, while the splice()
method changes the contents of an array by removing, replacing, or adding elements.
82. Explain the concept of event bubbling and event capturing in JavaScript. Event bubbling is the default behavior in which an event triggered on a child element propagates up through its parent elements. Event capturing is the opposite, where the event is captured at the parent level and then propagated down to the target element.
83. What is the purpose of the bind()
method in JavaScript? The bind()
method creates a new function that, when called, has its this
keyword set to a specific value.
84. How do you check if an object has a specific property in JavaScript? You can use the hasOwnProperty()
method or the in
operator to check if an object has a specific property.
85. What is the purpose of the charAt()
method in JavaScript? The charAt()
method returns the character at a specified index in a string.
86. How do you round a number to a specific decimal place in JavaScript? You can use the toFixed()
method to round a number to a specific decimal place.
87. Explain the concept of event handling in JavaScript. Event handling involves responding to user interactions with the web page by attaching event listeners to elements and executing code when those events occur.
88. What is the purpose of the isNaN()
function in JavaScript? The isNaN()
function is used to determine whether a value is NaN (Not-a-Number).
89. How do you convert a string to a date object in a specific format in JavaScript? You can use libraries like Moment.js or use the Date
object’s methods like getFullYear()
, getMonth()
, getDate()
, etc., to construct a date object from a string in a specific format.
90. What is the difference between a shallow copy and a deep copy of an array in JavaScript? A shallow copy of an array creates a new array with references to the original array’s elements, while a deep copy creates a new array with completely independent copies of the original array’s elements.
91. How do you check if a value is a number in JavaScript? You can use the typeof
operator to check if a value is of type 'number'
or use the isNaN()
function to check if it is a valid number.
92. What is the purpose of the Object.keys()
method in JavaScript? The Object.keys()
method returns an array of a given object’s own enumerable property names.
93. How do you create a copy of an object in JavaScript? You can create a copy of an object by using techniques like object spread syntax ({...obj}
), Object.assign()
, or JSON.parse(JSON.stringify(obj))
.
94. What is the difference between null
and undefined
in JavaScript? null
represents the intentional absence of any object value, while undefined
represents a variable that has been declared but has not been assigned a value.
95. Explain the concept of function hoisting in JavaScript. Function hoisting is a behavior in which function declarations are moved to the top of their scope during the compilation phase, allowing them to be used before they are declared.
96. What is the purpose of the decodeURI()
and encodeURIComponent()
functions in JavaScript? The decodeURI()
function decodes a Uniform Resource Identifier (URI), while encodeURIComponent()
encodes a URI component by replacing certain characters with their escape sequences.
97. How do you clone an array in JavaScript? You can clone an array using techniques like slice()
, concat()
, or the spread operator ([...arr]
).
98. What is the purpose of the toLocaleString()
method in JavaScript? The toLocaleString()
method returns a string representing the array elements using locale-specific formatting.
99. Explain the concept of object-oriented programming (OOP) in JavaScript. Object-oriented programming in JavaScript involves using objects, classes, and inheritance to organize and structure code, making it more modular and reusable.
100. How do you check if a string contains a specific substring in JavaScript? You can use the includes()
, indexOf()
, or regular expressions to check if a string contains a specific substring.
101. If I miss any, Please write in comments section 🙂
Make sure to study and understand these questions and their answers to prepare for your JavaScript interviews. Good luck!