JavaScript Interview Questions
JavaScript Interview Questions
1. What is JavaScript?
JavaScript is a high-level, interpreted programming language primarily used for
developing web applications. It enables dynamic and interactive content on web pages,
making them more engaging and user-friendly.
Both 'null' and 'undefined' represent the absence of value, but they are used in different
contexts. 'null' is an assigned value that indicates the absence of an object, while
'undefined' typically means that a variable has been declared but has not been assigned
a value.
23. What is the difference between shallow copy and deep copy in JavaScript?
Shallow copy and deep copy are methods used to create copies of objects or arrays:
● Shallow copy: Creates a new object or array with a new reference, but the nested
objects or arrays inside the original object or array are still referenced. Modifying
the nested objects or arrays will affect both the original and the copied object or
array.
● Deep copy: Creates a new object or array with new references for all nested
objects or arrays. Modifying the nested objects or arrays in the copied object or
array does not affect the original.
24. What is event capturing in JavaScript?
Event capturing is the phase of event propagation in the DOM where an event is
captured by the top-most parent element and then propagates down to the target
element. It is the opposite of event bubbling. During event capturing, event handlers
specified on parent elements are triggered before the event reaches the target element.
6. What are classes in ES6 and how are they different from constructor
functions?
Classes in ES6 provide a more straightforward and intuitive way to create objects and
define their behavior compared to constructor functions. They use the class keyword and
support methods, constructors, inheritance, and more. Classes are syntactical sugar
over JavaScript's prototype-based inheritance model and provide a more familiar syntax
for working with objects and inheritance.
12. What is the difference between forEach and map methods in ES6?
● The forEach method iterates over an array and executes a provided function for
each element. It is primarily used for performing an operation on each element of
an array.
● The map method also iterates over an array and executes a provided function for
each element, but it returns a new array with the results of the function calls. It is
commonly used for transforming or mapping values from an array to a new array.
13. What is the purpose of the import and export keywords in ES6 modules?
The import keyword is used to import functionality from other modules, allowing you to
use functions, classes, or variables defined in other files. The export keyword is used to
export functionality from a module, making it accessible to other modules.
14. What is the difference between named exports and default exports in ES6
modules?
● Named exports allow you to export multiple values from a module by explicitly
naming them. When importing these exports, you need to use the corresponding
names.
● Default exports allow you to export a single value from a module as the default
export. When importing the default export, you can choose any name you prefer.
15. What is the purpose of the Symbol data type in ES6?
Symbols are a new primitive data type introduced in ES6. They are unique and
immutable values that can be used as property keys, providing a way to create
non-enumerable properties and prevent accidental property name collisions.
16. What is the difference between function declarations and arrow functions
in ES6?
● Function declarations are hoisted and have their own this value. They are defined
using the function keyword.
● Arrow functions are not hoisted and lexically bind the value of this. They have a
more concise syntax using the => arrow notation.
17. What is the purpose of the Map and Set data structures in ES6?
● Map is a built-in object in ES6 that allows you to store key-value pairs, where
both the keys and values can be of any type. It provides an efficient way to store
and retrieve data based on unique keys.
● Set is a built-in object in ES6 that allows you to store unique values of any type. It
provides methods for adding, removing, and checking the existence of values.
18. What is the purpose of the Proxy object in ES6?
The Proxy object is a built-in object in ES6 that allows you to intercept and customize
fundamental operations on objects, such as property access, assignment, function
invocation, and more. It enables you to create custom behaviors for objects and provides
a powerful mechanism for metaprogramming.
20. What is the purpose of the async and await keywords in ES6?
The async and await keywords are used together to handle asynchronous operations in
a more synchronous-like manner. The async keyword is used to define an asynchronous
function, and the await keyword is used to pause the execution of an asynchronous
function until a promise is resolved or rejected. This simplifies the syntax for working with
promises and makes asynchronous code easier to read and write.