Top 100 JavaScript Interview Questions and Answers
Top 100 JavaScript Interview Questions and Answers
reload
o Increased interactivity − Create interactive interfaces that react to user actions
o Richer interfaces − Use JavaScript to add drag-and-drop components and sliders for richer interfaces
The code above shows that the array “arr” has not defined a property or method called push, yet no error is thrown
by the JavaScript engine.
The reason is due to the use of prototypes. Array objects inherit properties from the Array prototype, as discussed
previously.
When the JavaScript engine doesn’t find the push method on the current array object, it searches for it within the
Array prototype and finds it there.
If a property or method isn’t found on the current object, the JavaScript engine will search through its prototype
and continue to search through the prototype’s prototype until it finds the property or method or until there are no
more prototypes to search.
4. How do you write a for loop in JavaScript?
The syntax for a for loop in JavaScript is:
for (initialization; condition; update) { statement }
Client-side JavaScript consists of two main parts: a core language and predefined objects for executing JavaScript
in a browser environment.
JavaScript for the client is automatically incorporated into HTML pages and is interpreted by the browser at runtime.
Server-side JavaScript is similar to client-side JavaScript, but it includes JavaScript code that runs on a server and
is deployed only after processing.
19. What is the difference between function declaration and arrow function in JavaScript?
Function Declaration Arrow Function
Defined using the function keyword, followed by the function name, parameters, and Defined using the arrow => symbol, followed by the parameters and
the function body in curly braces. the function body.
Example: function square(x) { return x * x; } Example: const square = x => x * x;
Can be used as constructors with the new keyword. Cannot be used as constructors.
Has its own this keyword that refers to the current object. Inherit the this keyword from the enclosing scope.
Example: function Person(name, age) { this.name = name; Example: const Person = (name, age) => {
this.age = age; } this.name = name; this.age = age; };
Can be defined after they are called in the code. Must be defined before they are called in the code.
Example: console.log(square(5)); // Outputs 25<br> function Example: const square = x => x *
Function Declaration Arrow Function
square(x) { return x * x; } x;<br> console.log(square(5)); // Outputs 25
35. What is the difference between a for loop and a forEach loop in JavaScript?
A for loop is a traditional loop that iterates over an array or an object.
A forEach loop is a method on arrays that calls a provided function once for each element in the array.
37. What are the different methods for string manipulation in JavaScript?
Some of the common string methods in JavaScript are:
toUpperCase() and toLowerCase() for changing the case of a string.
concat() for concatenating two or more strings.
trim() for removing whitespace from the beginning and end of a string.
slice() and substring() for extracting parts of a string.
split() for splitting a string into an array of substrings based on a delimiter.
39. What is the difference between class and function components in React?
Class Components Function Components
Extends the React.Component class to create a component. A JavaScript function that returns a React element.
Can contain state and lifecycle methods. Can use hooks to contain state and lifecycle methods.
Defined using ES6 classes. Defined using JavaScript functions.
Uses this.props to access props. Uses props as an argument to access props.
Can use the constructor() method to set the initial state. Can use the useState() hook to set the initial state.
Can use the componentDidMount() method to perform actions after the component Can use the useEffect() hook to perform actions after the component is
is mounted. mounted.
Can use the render() method to return JSX. Returns JSX directly from the function body.
Example:<br>class MyComponent extends React.Component Example:<br>function MyComponent(props) {<br>return
{<br>render() {<br>return <h1>Hello, <h1>Hello, {props.name}!</h1>;<br>}<br>export default
{this.props.name}!</h1>;<br>}<br>} MyComponent;
42. What are the built-in methods and the values returned by them?
Built-in Method Values
CharAt() It returns the character at the specified index.
Concat() It joins two or more strings.
length() It returns the length of the string.
pop() It removes the last element from an array and returns that element.
forEach() It calls a function for each element in the array.
indexOf() It returns the index within the calling String object of the first occurrence of the specified value.
push() It adds one or more elements to the end of an array and returns the new length of the array.
reverse() It reverses the order of the elements of an array.
44. What is the difference between export and export default in JavaScript?
The export statement is used to export named bindings while the export default statement is used to export a
default binding.
With export, we can export multiple named bindings from a module, while with export default, we can only export
one default binding.
47. What is the difference between local and global variables in JavaScript?
Local variables are declared inside a function and can only be accessed within that function, while global variables
are declared outside of any function and can be accessed from anywhere in the code.
Local variables are only accessible while the function is executing, while global variables remain accessible
throughout the entire lifecycle of the application.
77. What is the difference between Object.assign() and spread operator in JavaScript?
Object.assign() is a method in JavaScript that copies the properties of one or more source objects into a target
object.
The spread operator (…) is a new feature in JavaScript that allows for the copying of arrays and objects into new
arrays or objects.
79. What is the difference between synchronous and asynchronous programming in JavaScript?
Synchronous programming in JavaScript is when each statement in the code is executed one after the other, in a
linear order.
Asynchronous programming in JavaScript is when certain operations (such as network requests or file I/O) are
performed in the background, without blocking the execution of the rest of the code.
81. What is the difference between the Event object and the EventTarget object in JavaScript?
The Event object in JavaScript represents an event that has occurred (such as a click or a keypress).
The EventTarget object represents an object that can receive events and has methods for adding and removing
event listeners.
82. What is the difference between event bubbling and event capturing in JavaScript?
Aspect Event Bubbling Event Capturing
When an event occurs on a nested element, it first trigg
When an event occurs on a nested element, it first triggers on
on the
Definition the most specific element and then triggers on
top of the DOM tree and then triggers on the ancestors
the ancestors until it reaches the top of the DOM tree
it reaches the most specific element
Direction Bottom-up Top-down
Default behavior Enabled Disabled
DOM Level Level 2 Level 2
Aspect Event Bubbling Event Capturing
The outermost element’s event handler is called
The innermost element’s event handler is called first and then the first and then
Event order outer elements’ event handlers are called in the inner elements’ event handlers are called
order up to the document’s event handler in order down to the target element’s event handler
Event phases Bubbling phase, Target phase, Capturing phase Capturing phase, Target phase, Bubbling phase
Registered using the addEventListener() method
Event listeners Registered using the addEventListener() method with the third parameter set to false (default) with
the third parameter set to true
83. What is the difference between bubbling and capturing in JavaScript event propagation?
Event propagation in JavaScript can occur in two ways: bubbling and capturing.
Bubbling is when an event is first captured by the innermost element, and then propagated up through its
ancestors.
Capturing is when an event is first captured by the outermost element, and then propagated down through its
descendants.
88. What is the difference between class and prototype-based inheritance in JavaScript?
Class-based inheritance is a way of creating objects that involves defining a class and creating instances of that
class.
Prototype-based inheritance is a way of creating objects by defining a prototype object and creating new objects
that inherit from that prototype.
90. What is the difference between the new keyword and Object.create() in JavaScript?
The new keyword is used to create a new instance of a constructor function.
Object.create() is used to create a new object that inherits from a specified prototype object.
91. What is the difference between the bind() and apply() methods in JavaScript?
The bind() method returns a new function with a bound this value and optional arguments.
The apply() method calls a function with a specified this value and arguments provided as an array.
93. What is the difference between var, let, and const in JavaScript?
var is function-scoped and can be redeclared and reassigned.
let and const are block-scoped and cannot be redeclared in the same block, but let can be reassigned while const
cannot be reassigned.
97. What is the difference between a function declaration and a function expression in JavaScript?
Function declarations are hoisted while function expressions are not.
Function declarations can be called before they are declared while function expressions cannot.
Function declarations create named functions while function expressions can be anonymous.
98. What is the difference between Local storage & Session storage?
Local Storage is a client-side storage mechanism that reduces traffic between the client and server by storing data
locally, and it persists until it is cleared manually through program settings.
Session Storage is similar to Local Storage, but the data stored in Session Storage gets cleared automatically when
the page session ends, while data stored in Local Storage has no expiration time.
If you encounter any difficulties with these JavaScript interview questions, please feel free to comment on your
challenges in the section below.