Top 25 JavaScript Interview Questions (2025)
1. What are JavaScript ES6+ features that are widely used in modern development?
Modern startups leverage ES6+ features for better code readability and performance:
- **Arrow Functions**: `const add = (a, b) => a + b;`
- **Template Literals**: Allows string interpolation: ``Hello, ${name}!``
- **Destructuring Assignment**: `const { name, age } = user;`
- **Spread & Rest Operator**: `const newArray = [...oldArray, newItem];`
- **Async/Await**: `const data = await fetchData();`
- **Optional Chaining (`?.`)**: `user?.profile?.email`
- **Nullish Coalescing (`??`)**: `const val = input ?? 'default';`
- **Modules (`import/export`)**: Allows better code organization.
2. What is the difference between `let`, `const`, and `var`?
- `var` is function-scoped and can be re-declared.
- `let` is block-scoped and prevents re-declaration.
- `const` is block-scoped and immutable (cannot be reassigned).
**Example:**
```js
var a = 10; // function-scoped
let b = 20; // block-scoped
const c = 30; // block-scoped & immutable
```
3. Explain event delegation and why it is useful.
Event delegation is a technique in which a parent element handles events for its child elements using event
bubbling.
**Example:**
```js
document.getElementById('parent').addEventListener('click', function(event) {
if (event.target.matches('.child-button')) {
console.log('Button Clicked!');
}
});
```
**Why useful?**
- Reduces memory usage by avoiding multiple event listeners.
- Handles dynamically added elements automatically.
4. What are Promises and how do they differ from Callbacks?
- **Promises** allow handling asynchronous operations in a cleaner way than callbacks.
**Example:**
```js
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve('Data Fetched!'), 2000);
});
};
fetchData().then(console.log).catch(console.error);
```
- Callbacks lead to 'callback hell', whereas Promises improve readability with `.then()` and `.catch()`.
5. What is async/await, and how does it simplify Promises?
`async/await` makes asynchronous code look synchronous, improving readability.
**Example:**
```js
async function fetchData() {
try {
const response = await fetch('https://api.example.com');
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
}
fetchData();
```
**Advantages:**
- Avoids `.then()` nesting.
- Reads like synchronous code.
- Uses `try/catch` for better error handling.