Interview Prep

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.

1–10: Basics & Array Creation

Q1 How do you create an array in JavaScript?
There are two primary ways:
  • Array literal: let arr = [1, 2, 3];
  • Array constructor: let arr = new Array(1, 2, 3);
The literal syntax is preferred for readability and performance.
Q2 How do you check if a variable is an array?
Use 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
Q3 What is the difference between 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 the new Array(n) pitfall.
Array.from('abc');        // ['a', 'b', 'c']
        Array.of(3);              // [3]
        new Array(3);            // [empty × 3]
Q4 How do you access the first and last element of an array?
  • 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
Q5 What is the length property of an array?
The 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]
Q6 How do you create an array with a fixed size filled with a default value?
Use Array(n).fill(value):
let arr = Array(5).fill(0);
        console.log(arr); // [0, 0, 0, 0, 0]
Q7 Can arrays hold different data types?
Yes, JavaScript arrays are heterogeneous — they can hold values of different types:
let mixed = [42, 'hello', true, null, { name: 'John' }];
Q8 What is a sparse array?
A sparse array has empty slots (holes). This happens when you create an array with 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.
Q9 How do you clone an array?
Several ways:
  • Spread: let copy = [...arr];
  • slice(): let copy = arr.slice();
  • Array.from(): let copy = Array.from(arr);
Note: These create shallow copies — nested objects are still shared.
Q10 How do you convert an array to a string?
Use 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

Q11 What is the difference between forEach() and map()?
  • forEach() executes a function for each element but does not return a new array (returns undefined).
  • 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
Q12 How does 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]
Q13 What does 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
Q14 What is the difference between 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]
Q15 How do you flatten a nested array?
Use 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.
Q16 What is 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']
Q17 How do you iterate over an array of objects?
You can use any iteration method like forEach(), map(), or a for...of loop:
let users = [{ name: 'Alice' }, { name: 'Bob' }];
        users.forEach(user => console.log(user.name));
Q18 How do you transform an array of numbers to their squares?
Use map():
let nums = [1, 2, 3, 4];
        let squares = nums.map(n => n ** 2);
        console.log(squares); // [1, 4, 9, 16]
Q19 What is the difference between some() and every()?
  • some() returns true if any element passes the test.
  • every() returns true if 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
Q20 How do you count the occurrences of a value in an array?
Use 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

Q21 How do you find an element in an array?
Use 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' }
Q22 What is the difference between find() and filter()?
  • find() returns the first matching element.
  • filter() returns an array of all matching elements.
Q23 How do you find the index of an element?
Use 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
Q24 How do you check if an array contains a value?
Use 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().
Q25 How do you find the largest number in an array?
Use 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).
Q26 How do you find the smallest number in an array?
Use Math.min() with spread:
let nums = [10, 5, 20, 8];
        console.log(Math.min(...nums)); // 5
Q27 How do you remove falsy values from an array?
Use filter() with Boolean:
let arr = [0, 1, false, 2, '', 3, null, undefined];
        let cleaned = arr.filter(Boolean);
        console.log(cleaned); // [1, 2, 3]
Q28 How do you find the intersection of two arrays?
Use 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]
Q29 How do you find the difference between two arrays?
Use 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]
Q30 How do you get unique values from an array?
Use 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

Q31 What is the difference between 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]
Q32 How do you add an element to the end of an array?
Use push():
let arr = [1, 2];
        arr.push(3);
        console.log(arr); // [1, 2, 3]
Q33 How do you remove the last element from an array?
Use pop():
let arr = [1, 2, 3];
        arr.pop();
        console.log(arr); // [1, 2]
Q34 How do you add an element to the beginning of an array?
Use unshift():
let arr = [2, 3];
        arr.unshift(1);
        console.log(arr); // [1, 2, 3]
Q35 How do you remove the first element from an array?
Use shift():
let arr = [1, 2, 3];
        arr.shift();
        console.log(arr); // [2, 3]
Q36 How do you reverse an array?
Use 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().
Q37 How do you sort an array of numbers?
Use 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.
Q38 How do you merge two arrays?
Use the spread operator or 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]
Q39 How do you replace an element at a specific index?
Use 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.
Q40 How do you remove an element at a specific index?
Use 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

Q41 How do you rotate an array by k steps?
Use 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]
Q42 How do you find the missing number in an array of 1 to n?
Use the sum formula: 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
Q43 How do you find duplicates in an array?
Use a 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]
Q44 How do you chunk an array into smaller arrays of a given size?
Use 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]]
Q45 How do you shuffle an array?
Use the Fisher–Yates (Knuth) shuffle:
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;
        }
Q46 How do you group objects by a property?
Use 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] }
Q47 How do you find the sum of all elements in an array?
Use reduce():
let nums = [1, 2, 3, 4];
        let sum = nums.reduce((acc, cur) => acc + cur, 0);
        console.log(sum); // 10
Q48 How do you find the average of an array?
Sum divided by length:
let nums = [2, 4, 6, 8];
        let avg = nums.reduce((a, b) => a + b, 0) / nums.length;
        console.log(avg); // 5
Q49 How do you remove a specific element from an array?
Use 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]
Q50 How do you check if an array is sorted?
Use 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.

Explore Course