Top 50 JavaScript Array Questions and Answer
Arrays are one of the most fundamental data structures in JavaScript. Mastering array operations is essential for any JavaScript developer — whether you're preparing for a technical interview or building real-world applications.
This comprehensive guide covers 50 frequently asked array questions, ranging from beginner-friendly concepts to advanced problem-solving techniques. Each question includes a clear explanation and practical code examples.
Quick Jump
1–10: Basics & Array Creation
- Array literal:
let arr = [1, 2, 3]; - Array constructor:
let arr = new Array(1, 2, 3);
Array.isArray() — it returns true if the value is an array.
let arr = [1, 2, 3];
console.log(Array.isArray(arr)); // true
console.log(Array.isArray({})); // false
Array.from() and Array.of()?Array.from()creates an array from an iterable or array-like object.Array.of()creates an array from a list of arguments, avoiding thenew Array(n)pitfall.
Array.from('abc'); // ['a', 'b', 'c']
Array.of(3); // [3]
new Array(3); // [empty × 3]
- First:
arr[0] - Last:
arr[arr.length - 1]
let arr = [10, 20, 30, 40];
console.log(arr[0]); // 10
console.log(arr[arr.length - 1]); // 40
length property of an array?length property returns the number of elements in an array. It is mutable — you can truncate or extend an array by assigning a new value.
let arr = [1, 2, 3];
console.log(arr.length); // 3
arr.length = 2;
console.log(arr); // [1, 2]
Array(n).fill(value):
let arr = Array(5).fill(0);
console.log(arr); // [0, 0, 0, 0, 0]
let mixed = [42, 'hello', true, null, { name: 'John' }];
new Array(n) or delete an element.
let sparse = new Array(3);
console.log(sparse.length); // 3
console.log(sparse[0]); // undefined (hole)
Sparse arrays can cause unexpected behavior with iteration methods.
- Spread:
let copy = [...arr]; slice():let copy = arr.slice();Array.from():let copy = Array.from(arr);
join() to specify a separator:
let arr = ['a', 'b', 'c'];
console.log(arr.join(',')); // 'a,b,c'
console.log(arr.join('')); // 'abc'
toString() also works but uses commas by default.
11–20: Iteration & Transformation
forEach() and map()?forEach()executes a function for each element but does not return a new array (returnsundefined).map()creates a new array by applying a function to each element.
let nums = [1, 2, 3];
let doubled = nums.map(n => n * 2); // [2, 4, 6]
nums.forEach(n => console.log(n)); // logs 1,2,3
filter() work?filter() creates a new array with all elements that pass a test (predicate) function.
let nums = [1, 2, 3, 4, 5];
let evens = nums.filter(n => n % 2 === 0);
console.log(evens); // [2, 4]
reduce() do?reduce() reduces an array to a single value by iteratively applying a reducer function. It takes an accumulator and current value.
let nums = [1, 2, 3, 4];
let sum = nums.reduce((acc, cur) => acc + cur, 0);
console.log(sum); // 10
map() and forEach() in terms of chaining?map() returns an array, so it can be chained with other methods like filter() or reduce(). forEach() returns undefined, so chaining is not possible.
let result = [1,2,3,4]
.map(n => n * 2)
.filter(n => n > 4);
console.log(result); // [6, 8]
flat() — it creates a new array with all sub-array elements concatenated up to the specified depth.
let nested = [1, [2, [3, 4]]];
console.log(nested.flat(1)); // [1, 2, [3, 4]]
console.log(nested.flat(2)); // [1, 2, 3, 4]
For deeply nested arrays, use Infinity as depth.
flatMap()?flatMap() maps each element using a function, then flattens the result by one level — it's equivalent to map().flat(1).
let arr = ['hello', 'world'];
let result = arr.flatMap(word => word.split(''));
console.log(result); // ['h','e','l','l','o','w','o','r','l','d']
forEach(), map(), or a for...of loop:
let users = [{ name: 'Alice' }, { name: 'Bob' }];
users.forEach(user => console.log(user.name));
map():
let nums = [1, 2, 3, 4];
let squares = nums.map(n => n ** 2);
console.log(squares); // [1, 4, 9, 16]
some() and every()?some()returnstrueif any element passes the test.every()returnstrueif all elements pass the test.
let nums = [2, 4, 6];
console.log(nums.some(n => n > 5)); // true
console.log(nums.every(n => n > 5)); // false
filter() with length, or reduce():
let arr = [1, 2, 2, 3, 2];
let count = arr.filter(v => v === 2).length;
console.log(count); // 3
// using reduce
let count2 = arr.reduce((acc, v) => acc + (v === 2 ? 1 : 0), 0);
21–30: Searching & Filtering
find() — it returns the first element that satisfies a condition, or undefined if none.
let users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
let user = users.find(u => u.id === 2);
console.log(user); // { id: 2, name: 'Bob' }
find() and filter()?find()returns the first matching element.filter()returns an array of all matching elements.
indexOf() (for primitive values) or findIndex() (for complex conditions):
let arr = ['a', 'b', 'c'];
console.log(arr.indexOf('b')); // 1
let users = [{ id: 1 }, { id: 2 }];
console.log(users.findIndex(u => u.id === 2)); // 1
includes() for primitive values:
let arr = [1, 2, 3];
console.log(arr.includes(2)); // true
console.log(arr.includes(4)); // false
For objects, use some().
Math.max() with the spread operator:
let nums = [10, 5, 20, 8];
console.log(Math.max(...nums)); // 20
Or use reduce() for large arrays (to avoid stack overflow).
Math.min() with spread:
let nums = [10, 5, 20, 8];
console.log(Math.min(...nums)); // 5
filter() with Boolean:
let arr = [0, 1, false, 2, '', 3, null, undefined];
let cleaned = arr.filter(Boolean);
console.log(cleaned); // [1, 2, 3]
filter() and includes():
let a = [1, 2, 3, 4];
let b = [3, 4, 5, 6];
let intersection = a.filter(x => b.includes(x));
console.log(intersection); // [3, 4]
filter() with includes():
let a = [1, 2, 3, 4];
let b = [3, 4, 5, 6];
let diff = a.filter(x => !b.includes(x));
console.log(diff); // [1, 2]
Set:
let arr = [1, 2, 2, 3, 3, 4];
let unique = [...new Set(arr)];
console.log(unique); // [1, 2, 3, 4]
31–40: Manipulation & Mutation
slice() and splice()?slice()returns a new array (shallow copy) without modifying the original.splice()modifies the original array by adding, removing, or replacing elements.
let arr = [1, 2, 3, 4];
let sliced = arr.slice(1, 3); // [2, 3] — arr unchanged
let spliced = arr.splice(1, 2); // [2, 3] — arr now [1, 4]
push():
let arr = [1, 2];
arr.push(3);
console.log(arr); // [1, 2, 3]
pop():
let arr = [1, 2, 3];
arr.pop();
console.log(arr); // [1, 2]
unshift():
let arr = [2, 3];
arr.unshift(1);
console.log(arr); // [1, 2, 3]
shift():
let arr = [1, 2, 3];
arr.shift();
console.log(arr); // [2, 3]
reverse() — it mutates the original array.
let arr = [1, 2, 3];
arr.reverse();
console.log(arr); // [3, 2, 1]
To reverse without mutation, use [...arr].reverse().
sort() with a compare function:
let nums = [3, 1, 4, 2];
nums.sort((a, b) => a - b); // ascending
console.log(nums); // [1, 2, 3, 4]
Without a compare function, sort() converts elements to strings, which gives unexpected results for numbers.
concat():
let a = [1, 2];
let b = [3, 4];
let merged = [...a, ...b]; // [1, 2, 3, 4]
let merged2 = a.concat(b); // [1, 2, 3, 4]
splice():
let arr = [1, 2, 3, 4];
arr.splice(2, 1, 99); // replace index 2 with 99
console.log(arr); // [1, 2, 99, 4]
Or use bracket notation: arr[2] = 99.
splice():
let arr = [1, 2, 3, 4];
arr.splice(2, 1); // remove index 2
console.log(arr); // [1, 2, 4]
41–50: Advanced & Problem-Solving
slice() and spread:
function rotate(arr, k) {
k = k % arr.length;
return [...arr.slice(-k), ...arr.slice(0, -k)];
}
console.log(rotate([1,2,3,4,5], 2)); // [4,5,1,2,3]
n*(n+1)/2 minus the sum of the array.
function missingNumber(arr, n) {
let total = (n * (n + 1)) / 2;
let sum = arr.reduce((a, b) => a + b, 0);
return total - sum;
}
console.log(missingNumber([1,2,4,5], 5)); // 3
Set or filter():
let arr = [1, 2, 2, 3, 4, 4];
let seen = new Set();
let duplicates = arr.filter(n => seen.has(n) ? true : (seen.add(n), false));
console.log(duplicates); // [2, 4]
slice() inside a loop:
function chunk(arr, size) {
let result = [];
for (let i = 0; i < arr.length; i += size) {
result.push(arr.slice(i, i + size));
}
return result;
}
console.log(chunk([1,2,3,4,5], 2)); // [[1,2],[3,4],[5]]
function shuffle(arr) {
let a = [...arr];
for (let i = a.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
reduce():
let people = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 25 }
];
let grouped = people.reduce((acc, person) => {
(acc[person.age] = acc[person.age] || []).push(person);
return acc;
}, {});
console.log(grouped); // { 25: [Alice, Charlie], 30: [Bob] }
reduce():
let nums = [1, 2, 3, 4];
let sum = nums.reduce((acc, cur) => acc + cur, 0);
console.log(sum); // 10
let nums = [2, 4, 6, 8];
let avg = nums.reduce((a, b) => a + b, 0) / nums.length;
console.log(avg); // 5
filter() (non-mutating) or splice() (mutating):
let arr = [1, 2, 3, 4];
// non-mutating
let filtered = arr.filter(n => n !== 3);
console.log(filtered); // [1, 2, 4]
// mutating
let index = arr.indexOf(3);
if (index !== -1) arr.splice(index, 1);
console.log(arr); // [1, 2, 4]
every():
function isSorted(arr) {
return arr.every((v, i) => i === 0 || v >= arr[i - 1]);
}
console.log(isSorted([1, 2, 3, 4])); // true
console.log(isSorted([1, 3, 2, 4])); // false
Pro Tip: Practice these questions with a code editor — writing actual code is the best way to build confidence for technical interviews.
Ready to master real-world JavaScript development?
Learn JavaScript hands-on with mentor-led, live sessions.