JavaScript Interview Question
JavaScript Interview Question
1. What is JavaScript?
Answer: JavaScript is a programming language used primarily to create interactive and dynamic
content on websites. It can be used for both client-side and server-side development.
Answer:
Answer: Hoisting is JavaScript's default behavior of moving declarations to the top of the current
scope before code execution. It applies to both variables and functions.
Answer: Arrow functions provide a shorter syntax for writing functions and do not have their own
this context, which helps avoid issues with scope in callbacks.
javascript
Copy code
const sum = (a, b) => a + b;
6. What is the difference between == and === in JavaScript?
Answer:
Answer: A closure is a feature in JavaScript where an inner function has access to its own scope,
the outer function’s scope, and the global scope. It helps in data encapsulation and maintaining
state.
Answer: Promises are used to handle asynchronous operations in JavaScript. A promise represents
a value which may be available now, in the future, or never.
fi
9. What is an IIFE (Immediately Invoked Function Expression)?
Answer: An IIFE is a function that runs immediately after it is de ned. It's useful for creating a new
scope to avoid variable pollution.
javascript
Copy code
(function() {
console.log("IIFE executed");
})();
10. Explain the concept of this keyword in JavaScript.
Answer: The this keyword refers to the object that is executing the current function. Its value
depends on how the function is called.
Answer: The event loop is responsible for handling asynchronous callbacks in JavaScript. It
continuously checks the call stack and the callback queue to process events and execute code.
Answer: async and await are used for handling promises more ef ciently. async makes a
function return a promise, and await makes the code wait for the promise to resolve.
Answer:
Answer: Prototypal inheritance in JavaScript is a way to add properties and methods to an object.
Objects inherit directly from other objects through prototypes.
Answer:
• call() and apply() are used to invoke a function with a speci c this value.
• bind() creates a new function with a xed this value.
16. Explain Event Delegation.
Answer: Event delegation is a technique to handle events ef ciently by attaching a single event
listener to a parent element to manage all child elements, using the event's bubbling phase.
fi
fi
fi
fi
fi
17. What are JavaScript Arrays and how do you manipulate them?
Answer: Arrays in JavaScript are used to store multiple values in a single variable. Methods like
push(), pop(), shift(), unshift(), map(), filter(), and reduce() are used
to manipulate arrays.
Answer: The spread operator (...) allows an iterable to be expanded in places where multiple
elements or arguments are expected.
javascript
Copy code
let arr = [1, 2, 3];
console.log(...arr); // 1 2 3
19. What is Destructuring in JavaScript?
Answer: Destructuring is a syntax that allows extracting values from arrays or properties from
objects into distinct variables.
javascript
Copy code
let [a, b] = [1, 2];
let {x, y} = {x: 10, y: 20};
20. What is JSON and how is it used?
Answer: JSON (JavaScript Object Notation) is a lightweight data interchange format. It is used to
store and transport data, often in API calls.
Answer: The DOM is a programming interface for HTML and XML documents, representing the
page's structure as a tree of objects that can be manipulated with JavaScript.
Answer: Events can be created using methods like addEventListener() and triggered
using dispatchEvent().
Answer:
Answer: Asynchronous JavaScript allows code to execute without blocking the main thread.
Techniques like callbacks, promises, and async/await are used for handling asynchronous
operations.
Answer: Higher-order functions are functions that take other functions as arguments or return them
as results.
Answer: Type coercion refers to the automatic or implicit conversion of values from one data type
to another (like converting strings to numbers).
Answer: Strict mode is a way to opt into a restricted variant of JavaScript, catching common
coding bugs and preventing unsafe actions.
Answer: The fetch() method is used to make network requests to APIs and returns a promise
that resolves to the response object.