Explore the top 17 JavaScript interview questions and answers designed for beginners. Learn about promises, event bubbling, currying, design patterns, and more in simple terms.
1. What are promise methods?
Promise methods help us manage promises. Here are some common ones:
- Promise.all(): Waits for all promises to finish and gives the result as an array. If one promise fails, it fails all.
- Promise.race(): Returns the result of the first promise that finishes (either success or failure).
- Promise.any(): Returns the first successful promise. If all fail, it gives an error.
- Promise.allSettled(): Waits for all promises to finish, whether they succeed or fail.
2. Difference between Promise.race()
and Promise.any()
Promise.race()
: Returns the result of the first promise to settle (success or failure).Promise.any()
: Only returns the first successful promise. If all fail, it gives an error.
3. What is event bubbling and event capturing?
- Event bubbling: When an event (like a click) starts on the innermost element and moves up to the outer elements.
- Event capturing: The event starts from the outermost element and moves down to the innermost element.
4. What is the event delegation concept in JavaScript?
Event delegation means attaching a single event listener to a parent element to handle events for its child elements. Example: If you have many buttons inside a div, instead of adding a click event to each button, you add it to the parent div.
5. Difference between localStorage, sessionStorage, and cookies
- localStorage: Stores data permanently until you delete it.
- sessionStorage: Stores data temporarily; it’s cleared when the browser is closed.
- cookies: Stores small amounts of data that are sent to the server with every request.
6. What is the immutability concept in JavaScript?
Immutability means you can’t change an object or value directly. Instead, you create a new object or value with changes. Example: Strings in JavaScript are immutable.
7. Explain the difference between Map and Set.
- Map: Stores key-value pairs, like an object. Keys can be anything (e.g., objects, strings).
- Set: Stores unique values only, no duplicates are allowed.
8. What is the code splitting concept in JavaScript?
Code splitting means breaking a big JavaScript file into smaller chunks. This helps load only the parts of the code you need, making the website faster.
9. Explain memorization concepts in JavaScript with examples.
Memoization means saving the result of a function for a specific input so that the next time, the result can be reused instead of recalculating it.
Example:
function add(a, b) {
let cache = {};
const key = `${a},${b}`;
if (cache[key]) {
return cache[key];
}
cache[key] = a + b;
return cache[key];
}
10. What is the strict mode in JavaScript?
Strict mode makes JavaScript run more securely by avoiding bad coding practices.
Example:
“use strict”;
x = 10; // This will throw an error because x is not declared.
11. Difference between call(), bind(), and apply() method with example
call()
: Calls a function with arguments passed one by one.apply()
: Similar tocall()
, but arguments are passed as an array.bind()
: Returns a new function with a giventhis
value.
Example:
const obj = { name: “John” };
function greet(greeting) {
console.log(`${greeting}, ${this.name}`);
}
greet.call(obj, “Hello”); // Hello, John
greet.apply(obj, [“Hi”]); // Hi, John
const boundGreet = greet.bind(obj, “Hey”);
boundGreet(); // Hey, John
12. What is the currying concept in JavaScript?
Currying is breaking a function with multiple arguments into smaller functions that take one argument at a time.
Example:
function add(a) {
return function (b) {
return a + b;
};
}
const add5 = add(5);
console.log(add5(3)); // 8
13. Difference between shallow and deep copy in objects
- Shallow copy: Copies only the first level of an object. Changes in nested objects affect the original.
- Deep copy: Copies everything, including nested objects, so changes in the copy don’t affect the original.
14. Explain Promise chaining concept with example
Promise chaining means handling multiple promises one after another.
Example:
fetch(“https://api.example.com/data”)
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));
15. Explain Object.freeze and Object.seal method
Object.freeze()
: Makes an object immutable (you cannot add, remove, or change properties).Object.seal()
: You can update properties but cannot add or delete them.
16. Explain design patterns in JavaScript
Design patterns are reusable solutions for common problems in coding. Examples:
- Singleton: Only one instance of a class.
- Observer: Notifies other parts of the code when something changes.
17. Explain the concept of debouncing and throttling
- Debouncing: Delays a function call until there’s a pause in events.
Example: Search suggestions while typing. - Throttling: Ensures a function runs only once in a specific time interval.
Example: Tracking scroll events.