π Ultimate Guide: JavaScript Interview Questions and Answers (2025 Edition) - Part 2
Welcome to Part 2 of our JavaScript Interview Questions series! In this continuation, we dive deeper into advanced JavaScript concepts that are often asked in technical interviews. Prepare yourself with these crucial questions and answers to ace your next interview. π‘
Let's get started! π₯
π’ Advanced JavaScript Interview Questions
1. What is the difference between Object.freeze()
and Object.seal()
?
Method | Allows adding properties? | Allows modifying properties? | Allows deleting properties? |
---|---|---|---|
Object.freeze(obj) |
β No | β No | β No |
Object.seal(obj) |
β No | β Yes | β No |
const obj = { name: "Atul" };
Object.freeze(obj);
obj.name = "John"; // No effect
console.log(obj.name); // "Atul"
2. What is the difference between function declaration and function expression?
- Function Declaration: Can be hoisted and used before its definition.
- Function Expression: Assigned to a variable and not hoisted.
console.log(add(2, 3)); // Works!
function add(a, b) { return a + b; }
console.log(subtract(2, 3)); // Error!
const subtract = function(a, b) { return a - b; };
3. Explain the concept of currying in JavaScript.
Currying is a technique where a function takes multiple arguments one at a time, returning a new function for each argument.
function curry(a) {
return function (b) {
return function (c) {
return a + b + c;
};
};
}
console.log(curry(1)(2)(3)); // 6
4. What is a generator function in JavaScript?
A generator function (function*
) returns an iterator, allowing pause and resume execution using yield
.
function* generatorFunction() {
yield 1;
yield 2;
yield 3;
}
const gen = generatorFunction();
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
5. What is the difference between map()
, forEach()
, and filter()
?
Method | Returns new array? | Modifies original array? | Use Case |
---|---|---|---|
map() |
β Yes | β No | Transform values |
forEach() |
β No | β No | Loop through elements |
filter() |
β Yes | β No | Filter values |
const numbers = [1, 2, 3, 4, 5];
console.log(numbers.map(n => n * 2)); // [2, 4, 6, 8, 10]
numbers.forEach(n => console.log(n)); // Logs each number
console.log(numbers.filter(n => n > 2)); // [3, 4, 5]
6. What is memoization in JavaScript?
Memoization is an optimization technique to cache function results and improve performance.
function memoize(fn) {
let cache = {};
return function (arg) {
if (cache[arg]) return cache[arg];
cache[arg] = fn(arg);
return cache[arg];
};
}
const square = memoize(x => x * x);
console.log(square(4)); // 16 (calculated)
console.log(square(4)); // 16 (cached)
7. Explain the difference between deep copy and shallow copy.
- Shallow Copy: Copies only the first level; nested objects are referenced.
- Deep Copy: Fully copies all levels of an object.
let obj1 = { a: 1, b: { c: 2 } };
let shallowCopy = { ...obj1 }; // Shallow copy
let deepCopy = JSON.parse(JSON.stringify(obj1)); // Deep copy
8. What is the difference between synchronous and asynchronous programming?
Type | Execution | Blocking? |
---|---|---|
Synchronous | Sequential | β Yes |
Asynchronous | Parallel (non-blocking) | β No |
console.log("Start");
setTimeout(() => console.log("Async Call"), 1000);
console.log("End");
// Output: Start β End β Async Call (after 1s)
9. How does async/await
work in JavaScript?
-
async
makes a function return a promise. -
await
pauses execution until the promise resolves.
async function fetchData() {
let response = await fetch("https://jsonplaceholder.typicode.com/posts/1");
let data = await response.json();
console.log(data);
}
fetchData();
10. What is the new
keyword in JavaScript?
The new
keyword creates an instance of an object from a constructor function.
function Person(name) {
this.name = name;
}
const person = new Person("Atul");
console.log(person.name); // "Atul"
11. What is destructuring in JavaScript?
Destructuring extracts values from arrays or objects into variables.
const person = { name: "Atul", age: 25 };
const { name, age } = person;
console.log(name, age); // "Atul", 25
12. What is the difference between null
and undefined
?
Value | Meaning |
---|---|
null |
Intentional absence of a value |
undefined |
Uninitialized variable or missing property |
let x;
console.log(x); // undefined
x = null;
console.log(x); // null
π― Final Thoughts
This Part 2 guide covers more in-depth JavaScript interview questions that will prepare you for technical rounds. π
π¬ Got more questions? Drop them in the comments! π
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.