No products in the cart.
Here are some common JavaScript data structures interview questions along with their answers:
1. What are the main data structures in JavaScript?
JavaScript primarily supports the following data structures:
- Arrays: Ordered collections of values.
- Objects: Collections of key-value pairs.
- Sets: Collections of unique values (ES6).
- Maps: Collections of key-value pairs with keys of any type (ES6).
2. How do you create an array in JavaScript?
We can create an array using the array literal syntax or the Array constructor.
// Array literal
let array1 = [1, 2, 3];
// Array constructor
let array2 = new Array(4, 5, 6);3. How do you remove duplicates from an array?
We can use a Set to remove duplicates
const uniqueArray = [...new Set(array)];4. How do you check if an object is empty?
We can check if an object has no own properties using Object.keys()
const isEmpty = (obj) => Object.keys(obj).length === 0;
console.log(isEmpty({})); // true
console.log(isEmpty({ a: 1 })); // false5. What is the difference between map() and forEach()?
map()creates a new array populated with the results of calling a provided function on every element in the calling array. It returns a new array.forEach()executes a provided function once for each array element but does not return a new array; it returnsundefined.
6. How can you reverse an array in JavaScript?
We can reverse an array using the reverse() method.
let arr = [1, 2, 3];
arr.reverse(); // arr is now [3, 2, 1]
