JavaScript Array Interview Questions & Answers
1. What is an array in JavaScript?
An array is a special variable that can store multiple values in a single variable. Arrays are
ordered, zero-indexed, and can store any data type.
Example:
let fruits = ['apple', 'banana', 'mango'];
console.log(fruits[0]); // apple
2. How to check if a variable is an array?
Using Array.isArray() or instanceof Array.
Example:
let arr = [1,2,3];
console.log(Array.isArray(arr)); // true
console.log(arr instanceof Array); // true
3. How to add or remove elements from an array?
Add: push() (end), unshift() (start)
Remove: pop() (end), shift() (start)
Example:
let arr = [1,2,3];
arr.push(4); // [1,2,3,4]
arr.pop(); // [1,2,3]
arr.unshift(0); // [0,1,2,3]
arr.shift(); // [1,2,3]
4. How to loop through an array?
for loop, for...of, forEach()
Example:
let arr = [10,20,30];
for(let i=0; i<arr.length; i++) console.log(arr[i]);
for(let value of arr) console.log(value);
arr.forEach(val => console.log(val));
5. How to find an element in an array?
Using indexOf(), includes(), find(), findIndex()
Example:
let arr = [1,2,3,4];
console.log(arr.indexOf(3)); // 2
console.log(arr.includes(4)); // true
console.log(arr.find(x => x > 2)); // 3
6. How to remove duplicates from an array?
Using Set or filter()
Example:
let arr = [1,2,2,3,3,4];
let unique = [...new Set(arr)];
console.log(unique); // [1,2,3,4]
7. How to merge two arrays?
Using concat() or spread operator
Example:
let a = [1,2]; let b = [3,4];
let merged = a.concat(b); // [1,2,3,4]
let merged2 = [...a,...b]; // [1,2,3,4]
8. How to sort an array?
Using sort() method. For numbers, provide a compare function.
Example:
let arr = [3,1,4,2];
arr.sort((a,b)=>a-b); // [1,2,3,4]
9. How to filter elements in an array?
Using filter()
Example:
let arr = [1,2,3,4,5];
let even = arr.filter(x=>x%2===0);
console.log(even); // [2,4]
10. How to map elements of an array?
Using map() to transform elements
Example:
let arr=[1,2,3];
let squares=arr.map(x=>x*x);
console.log(squares); // [1,4,9]
11. How to reduce an array to a single value?
Using reduce()
Example:
let arr=[1,2,3,4];
let sum=arr.reduce((acc,curr)=>acc+curr,0);
console.log(sum); // 10
12. Difference between map(), forEach(), and filter()
map() → returns new array, transform elements
forEach() → undefined, iterate with side effects
filter() → returns new array, select elements
13. How to check if all or some elements satisfy a condition?
every() → all elements satisfy
some() → at least one satisfies
Example:
let arr=[2,4,6];
arr.every(x=>x%2===0); // true
arr.some(x=>x>5); // true
14. How to flatten nested arrays?
Using flat()
Example:
let arr=[1,[2,3],[4,[5]]];
arr.flat(2); // [1,2,3,4,5]
15. How to find duplicates in an array?
Using filter() and indexOf()
Example:
let arr=[1,2,3,2,4,3];
let duplicates = arr.filter((item,index)=>arr.indexOf(item)!==index);
console.log(duplicates); // [2,3]
16. How to copy an array?
Using slice(), concat(), or spread operator
Example:
let arr=[1,2,3];
let copy1=arr.slice(); let copy2=[...arr];
17. How to convert array to string and string to array?
join() → array to string, split() → string to array
Example:
let arr=[1,2,3]; arr.join('-'); // '1-2-3'
let str='a,b,c'; str.split(','); // ['a','b','c']
18. How to find max and min in an array?
Using Math.max(...arr) and Math.min(...arr)
Example:
let arr=[1,5,3];
Math.max(...arr); //5, Math.min(...arr); //1
19. Difference between splice() and slice()
splice() → modifies original array, returns removed elements
slice() → does not modify, returns shallow copy
20. Common interview array questions
Difference between map() and forEach(), slice() vs splice(), remove duplicates, flatten
nested arrays, filter() vs find(), sorting numbers
1️⃣ Difference between map() and forEach()
Feature map() forEach()
Returns New array undefined
Purpose Transform each element Iterate for side effects
Chainable Yes No
Original array Not modified Not modified
Example:
let arr = [1,2,3];
let squares = arr.map(x => x*x); // [1,4,9]
arr.forEach(x => console.log(x)); // logs 1,2,3
2️⃣ Difference between slice() and splice()
Feature slice() splice()
Modifies original array No Yes
Returns Selected elements as new array Removed elements
Parameters slice(start, end) splice(start, deleteCount, items...)
Use case Copy or extract subarray Add/remove elements in place
Example:
let arr = [1,2,3,4];
arr.slice(1,3); // [2,3], arr unchanged
arr.splice(1,2); // [2,3], arr now [1,4]
3️⃣ How to remove duplicates from array
Using Set or filter()
Example:
let arr = [1,2,2,3,3,4];
let unique = [...new Set(arr)]; // [1,2,3,4]
4️⃣ How to flatten nested arrays
Using flat()
Example:
let arr = [1,[2,3],[4,[5]]];
arr.flat(); // [1,2,3,4,[5]]
arr.flat(2); // [1,2,3,4,5]
5️⃣ Difference between filter() and find()
Feature filter() find()
Returns New array of all matching elements First matching element
Length Can be 0 or more Single value or undefined
Use case Get all matching Get one element
Example:
let arr = [1,2,3,4];
arr.filter(x => x>2); // [3,4]
arr.find(x => x>2); // 3
6️⃣ How to sort numbers in ascending/descending order
Using sort() with compare function
Example:
let arr = [3,1,4,2];
arr.sort((a,b)=>a-b); // ascending: [1,2,3,4]
arr.sort((a,b)=>b-a); // descending: [4,3,2,1]