ES6 Interview Questions and Answers
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. Explain the differences between let, const, and var.
let and const are block-scoped, while var is function-scoped. const is used for constants and cannot be reassigned.
Example:
const PI = 3.14; let x = 10; var y = 5;
Ques 2. What is destructuring assignment in ES6?
Destructuring assignment allows you to extract values from arrays or objects and assign them to variables.
Example:
const person = { name: 'John', age: 30 }; const { name, age } = person;
Ques 3. Explain the concept of spread/rest operator in ES6.
The spread operator (...) is used to spread elements of an array or object, while the rest operator is used to collect elements into an array.
Example:
const arr = [1, 2, 3]; const newArr = [...arr, 4, 5];
Ques 4. Explain the concept of promises in ES6.
Promises are used for asynchronous programming. They represent a value that may be available now, or in the future, or never.
Example:
const fetchData = new Promise((resolve, reject) => { /* async operation */ });
Ques 5. What is the 'async/await' feature in ES6?
Async/await is a syntax sugar for working with promises. It makes asynchronous code look and behave more like synchronous code.
Example:
async function fetchData() { const result = await fetch('https://example.com'); }
Ques 6. Explain the concept of modules in ES6.
Modules allow you to split your code into multiple files. You can export and import functionality between modules.
Example:
export const PI = 3.14; import { PI } from './math';
Ques 7. What is the purpose of the 'reduce' function in ES6?
The 'reduce' function is used to accumulate the elements of an array into a single value.
Example:
const numbers = [1, 2, 3, 4, 5]; const sum = numbers.reduce((acc, num) => acc + num, 0);
Ques 8. Explain the concept of the 'Symbol' data type in ES6.
Symbols are a new primitive data type in ES6, used to create unique identifiers.
Example:
const mySymbol = Symbol('description');
Ques 9. What is the purpose of the 'Set' data structure in ES6?
A Set is a collection of values with no duplicate entries. It is iterable and can store various types of values.
Example:
const uniqueNumbers = new Set([1, 2, 3, 2, 1]);
Ques 10. What is the purpose of the 'Array.findIndex()' method in ES6?
Array.findIndex() is used to find the index of the first element in an array that satisfies a given condition.
Example:
const numbers = [1, 2, 3, 4, 5]; const index = numbers.findIndex(num => num > 2);
Most helpful rated by users: