Top 30 JavaScript Interview Questions and Answers For 2024 - by Ravi Sharma - Medium
Top 30 JavaScript Interview Questions and Answers For 2024 - by Ravi Sharma - Medium
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 1/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
1.2K 17
Whether you’re a seasoned developer or just starting your career in tech, this
essential resource for 2024 will help you brush up on core concepts, from basic
language features to advanced topics.
In this article, I’ve compiled over 30 of the most crucial JavaScript interview
questions along with detailed answers and code examples.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 2/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
Dive into this invaluable collection to ace your JavaScript interviews and stay
ahead in today’s competitive tech industry”
Level-1: Basic
1. Is Javascript single-threaded?
2. Explain the main component of the JavaScript Engine and how it works.
8. What is async/await ?
Level-2 : Intermediate
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 3/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
17. What is the difference between Call, Apply, and Bind methods?
Level-3: Expert
21. What is Execution context, execution stack, variable object, scope chain?
24. Different ways to clone (Shallow and deep copy of object) an object?
26. What is Event and event flow, event bubbling and event capturing?
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 4/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
=============================================
1. Is Javascript single-threaded?
Every browser has a Javascript engine that executes the javascript code and
converts it into machine code.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 5/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
When JavaScript code is executed, the parser first reads the code and produces
an AST, and stores it in memory. The interpreter then processes this AST and
generates bytecode or machine code, which is executed by the computer.
JS Engine usually optimizes “hot functions” and uses inline caching techniques
to optimize the code.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 6/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
During this process, the call stack keeps track of the currently executing
functions, and the memory heap is used for memory allocation.
Finally, the garbage collector comes into play to manage memory by reclaiming
memory from unused objects.
3. Apart from Parser, there is a “pre-parser” that checks for syntax and tokens
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 7/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
event loop works by continuously monitoring two queues: the call stack and the
event queue.
Web APIs is the place where the async operations (setTimeout, fetch requests,
promises) with their callbacks are waiting to complete. It borrows the thread
from the thread pool to complete the task in the background without
blocking the main thread.
The job queue (or microtasks) is a FIFO (First In, First Out) structure
that holds the callbacks of async/await, promises,
process.nextTick() that are ready to be executed. For example, the resolve or
reject callbacks of a fulfilled promise are enqueued in the job queue.
The task queue (or macrostasks) is a FIFO (First In, First Out)
structure that holds the callbacks of async operations (timer like
setInterval, setTimeout) that are ready to be executed. For example, the
callback of a timed-out setTimeout() — ready to be executed — is enqueued in
the task queue.
The Event loop permanently monitors whether the call stack is empty. If
the call stack is empty, the event loop looks into the job queue or task queue
and dequeues any callback ready to be executed into the call stack.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 8/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
In a browser the window object is the window of the browser, the top
structure in the HTML tree. Variables declared with var globally are
attached to the window object. Type var dog = ‘bowser’ in the browser’s
console and then type window.dog. The value ‘bowser’ appears! This makes
controlling the scope of the variable even more difficult. By contrast, let and
const are not attached to the window object.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 9/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
Primitive data types in JavaScript are the most basic data types that represent
single values. They are immutable (cannot be changed) and directly
hold a specific value.
When a Symbol is used as a property key, it doesn’t clash with other property
keys, including string keys.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 10/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
function processData(data) {
console.log('Processing data:', data);
}
fetchData('https://example.com/data', processData);
In this example, the fetchData function takes a URL and a callback function
as arguments. After fetching the data from the server (simulated using
setTimeout), it calls the callback function and passes the retrieved data to it.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 11/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 12/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
readFile('file1.txt')
.then((data1) => {
return readFile('file2.txt');
})
.then((data2) => {
return readFile('file3.txt');
})
.then((data3) => {
// Continue with more promise-based code...
})
.catch((err) => {
console.error(err);
});
1. Pending: The initial state. This is the state in which the Promise’s eventual
value is not yet available.
2. Fulfilled: The state in which the Promise has been resolved successfully,
and the eventual value is now available.
3. Rejected: The state in which the Promise has encountered an error or has
been rejected, and the eventual value cannot be provided.
If an error occurred then call the reject function and pass the error to it.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 13/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
// Creating a Promise
const fetchData = new Promise((resolve, reject) => {
// Simulate fetching data from a server
setTimeout(() => {
const data = 'Some data from the server';
// Resolve the Promise with the retrieved data
resolve(data);
// Reject the Promise with an error
// reject(new Error('Failed to fetch data'));
}, 1000);
});
8. What is async/await ?
Async/await is a modern approach to handling asynchronous code in
JavaScript. It provides a more concise and readable way to work with Promises
and async operations, effectively avoiding the “Callback Hell” and improving the
overall structure of asynchronous code.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 15/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
// Handle errors
});
In summary, == checks for equality after type coercion, whereas === checks for
strict equality, considering both the values and their data types.
0 == false // true
0 === false // false
1 == "1" // true
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 16/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
1 === "1" // false
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false
[]==[] or []===[] //false, refer different objects in memory
{}=={} or {}==={} //false, refer different objects in memory
In JavaScript, there are several ways to create objects. Some common methods
for object creation include:
let person = {
firstName: 'John',
lastName: 'Doe',
greet: function() {
return 'Hello, ' + this.firstName + ' ' + this.lastName;
}
};
let personProto = {
greet: function() {
return 'Hello, ' + this.firstName + ' ' + this.lastName;
}
};
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
greet() {
return 'Hello, ' + this.firstName + ' ' + this.lastName;
}
}
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 18/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
let personProto = {
greet: function() {
return 'Hello, ' + this.firstName + ' ' + this.lastName;
}
};
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 19/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
source objects to a target object. This is particularly useful for merging objects
or creating a shallow copy.
let target = { a: 1, b: 2 };
let source = { b: 3, c: 4 };
let mergedObject = Object.assign({}, target, source);
function Animal(name) {
this.name = name;
}
Animal.prototype.greet = function() {
return 'Hello, I am ' + this.name;
};
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 20/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
function createInstance() {
return {
// properties and methods
};
}
return {
getInstance: () => {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3, 4)); // Outputs 10
The spread operator, also denoted by three dots ( ... ), is used to spread
the elements of an array or object into another array or object. It
allows you to easily clone arrays, concatenate arrays, and merge objects.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 21/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const mergedObject = { ...obj1, ...obj2 };
// mergedObject is { a: 1, b: 3, c: 4 }
function double(x) {
return x * 2;
}
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 22/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
Closure is a feature that allows the function to capture the environment (or to
retain access to variables from the scope ) where it is defined, even after
that scope has closed.
In other words, a closure gives a function access to its own scope, the scope of
its outer function, and the global scope, allowing it to “remember” and continue
to access variables and parameters from these scopes.
function outerFunction() {
let outerVariable = 'I am from the outer function';
return innerFunction() {
console.log(outerVariable); // Accessing outerVariable from the outer function'
}
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 23/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
execution stack. Once function execution completed it is popped off from stack.
Every execution context has a space in memory where its variables and
function are stored, and once the function popped off from the execution stack
a JavaScript Garbage collector clear all of these things.
In the above example, the anonymous execution context still has a reference to
the variables to the memory space of its outer environment. Even though the
outerFunction() is finished. (It can access the outerVariable variable and use
it inside console.log(outerVariable)).
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 24/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
When you declare a variable using var , the declaration is hoisted to the top of
its containing function or block and initialized with the default value of
“undefined”.
Variables declared with let and const are hoisted as well but have a "temporal
dead zone" where they cannot be accessed before their declaration.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 25/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
Function declarations are also hoisted to the top of their containing scope. You
can call a function before it’s declared in your code.
When you declare a variable with let or const , it is hoisted to the top of its
containing scope, However, unlike var , variables declared
with let and const remain uninitialized in the TDZ.
Any attempt to access or use the variable before its actual declaration within the
scope will result in a ReferenceError . This is to prevent the use of variables
before they have been properly defined.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 26/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
When you access a property or method on an object, JavaScript first looks for it
on the object itself. If it doesn’t find it, it looks up the prototype chain until it
finds the property or method. This process continues until it reaches
the Object.prototype at the top of the chain.
17. What is the difference between Call, Apply, and Bind methods?
Call: The call() method invokes a function with a specified this value
and individual arguments passed as comma-separated values
function greet(greeting) {
console.log(greeting + ' ' + this.name);
}
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 27/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
With call() method an object can use a method belonging to another object.
const o1 = {
name: 'ravi',
getName: function(){
console.log(`Hello, ${this.name}`)
}
}
const o2 = {
name: 'JavaScript Centric'
}
Apply: Invokes the function with a given this value but it accepts
arguments as an array. It is useful when the number of arguments to be
passed is not known in advance or when the arguments are already in an array.
bind: instead of invoking it returns a new function and allows you to pass any
number of arguments. bind() method takes an object as first argument and
create a new function.
const module = {
x: 42,
getX: function() {
return this.x;
}
};
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 28/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
1. Regular Functions
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 29/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
There are certain differences between Arrow and Regular function, i.e
1. Syntax
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 30/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
ES6, also known as ECMAScript 2015, introduced several new features and
enhancements to JavaScript, significantly expanding the language’s capabilities.
Some key features of ES6 include:
1. Arrow Functions
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 31/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
2. Block-Scoped Variables
3. Classes
4. Modules
6. Default Parameters
8. Destructuring Assignment
9. Promises
10. Map, Set, WeakMap, WeakSet: ES6 introduced new built-in data
structures, such as Map and Set, for more efficient and specialized handling
of collections and key-value pairs.
Execution Stack: It is also known as the “call stack,” a LIFO (Last in, First
out) data structure that stores all the execution context of the function calls that
are in progress. When a function is called, a new execution context is created
and pushed onto the stack. When the function completes, its context is popped
off the stack.
The engine executes the function whose execution context is at the top of the
stack. When this function completes, its execution stack is popped off from the
stack, and the control reaches the context below it in the current stack.
The execution context is created during the creation phase. The following things
happen during the creation phase:
Variable Object: It is a part of the execution context that contains all the
variables, function declarations, and arguments defined in that context.
Scope Chain: The scope chain is a mechanism for resolving the value of a
variable in JavaScript. When a variable is referenced, the JavaScript engine
looks for the variable first in the current execution context’s variable object. If
it’s not found there, it continues to the next outer execution context, following
the scope chain, until it finds the variable or reaches the global execution
context.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 33/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
The priority of execution can be understood based on the event loop and the
order in which different asynchronous operations are handled:
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 34/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
}
};
}
The function’s code can be paused within the body using the yield keyword,
and it can later be resumed from the exact point where it was paused.
function* numberGenerator() {
let i = 0;
while (true) {
yield i++;
}
}
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 35/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
24. Different ways to clone (Shallow and deep copy of object) an object?
A shallow copy is a copy of an object whose references are the same as the
original object. This means that if you change the value of a property in the
shallow copy, it will also change the value of the property in the original object.
const user = {
name: "Kingsley",
age: 28,
job: "Web Developer"
}
const clone = user
A deep copy is a copy of an object whose references are not the same as
the original object. This means that if you change the value of a property in the
deep copy, it will not change the value of the property in the original object.
b)structuredClone:
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 36/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
c)Spread Operator(…): any object with a nested object will not be deep
copied.
deepCopy.name = "ravi"
console.log("originalObject", originalObject.name) // Alice
e)Recursion:
function deepCopy(obj) {
if (typeof obj !== 'object' || obj === null) {
return obj;
}
const newObj = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (Object.hasOwnProperty.call(obj, key)) {
newObj[key] = deepCopy(obj[key]);
}
}
return newObj;
}
const originalObject = { name: "Alice", nested: { age: 25 } };
const deepCopy = deepCopy(originalObject);
In JavaScript, you can make an object immutable using the Object.seal() and
Object.freeze() methods.
26. What is Event and event flow, event bubbling and event capturing?
In JavaScript, Event flow is the order in which an event like a click or a keypress
is received on the web page or handled by the web browser. There are two
phases in event flow: event capturing and event bubbling.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 38/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
When you click an element that is nested in various other elements, before your
click actually reaches its destination or target element, it must trigger the
click event for each of its parent elements first, starting at the top with
the global window object.
<div id="parent">
<button id="child">Click me!</button>
</div>
1. Event Capturing Phase: When you click the button, the event starts its
journey from the top (the root of the document) and moves down to the
target element. In this case, it travels from the document’s root to
the <div> (parent element), then to the <button> (child element). This is
called the capturing phase.
2. Event Target Phase: The event reaches the target element, which is
the <button> in this case.
3. Event Bubbling Phase: After reaching the target, the event starts
bubbling up. It goes from the <button> back to the <div> and eventually to
the root of the document. This is called the bubbling phase.
document.getElementById('parent').addEventListener('click', function() {
console.log('Div clicked (capturing phase)');
}, true); // The 'true' here indicates capturing phase.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 39/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
document.getElementById('child').addEventListener('click', function() {
console.log('Button clicked (target phase)');
});
document.getElementById('parent').addEventListener('click', function()
{
console.log('Div clicked (bubbling phase)');
});
When you click the button, you’ll see these messages in the console in the
following order:
When an event occurs on one of the descendant elements, it “bubbles up” to the
common ancestor, where the event listener is waiting.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 40/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
SSE allows the server to push data to the web client (usually a browser) as soon
as new information is available, making it an excellent choice for scenarios
where you need real-time updates without relying on complex protocols or
third-party libraries.
b)SSE uses a text-based protocol, which means that data sent from the
server to the client is typically in a text format (usually JSON or plain text).
d)SSE establishes a persistent connection between the client and the server,
allowing the server to send a stream of events to the client. Each event can have
a unique type and data associated with it.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 41/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
f)Below are the list of events (onopen, onmessage,onerror) available for server-
sent events.
Web Workers and Service Workers are two different concepts in JavaScript,
Web Workers:
2. Use Cases: Web Workers are commonly used for tasks that are
computationally intensive or time-consuming, such as data processing,
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 42/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
Service Workers:
1. Offline Capabilities: Service Workers are a more advanced feature used
for creating Progressive Web Apps (PWAs). They act as proxy servers that
run in the background and can intercept and cache network requests. This
enables offline capabilities, such as serving cached content when the user is
offline.
2. Use Cases: Service Workers are primarily used for implementing features
like offline access, push notifications, and background sync. They
enable web apps to function even when there’s no internet connection.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 43/44
5/28/24, 8:52 PM Top 30 JavaScript Interview Questions and Answers for 2024 | by Ravi Sharma | Medium
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
console.log(areEqual(obj1, obj2)); // Output: true
b) You can use the Ramda library to compare two JSON objects as well. Ramda
provides a function called equals for this purpose.
const R = require('ramda');
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
c) Another option is to use a library, such as Lodash, that provides a method for
deep comparison of objects.
const _ = require('lodash');
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
console.log(_.isEqual(obj1, obj2)); // Output: true
Hope You Like this article. Big Thank you For Reading.
https://javascriptcentric.medium.com/top-30-javascript-interview-questions-and-answers-for-2024-7f1e2d1d0638 44/44