JavaScript Array Interview Questions

CodeyMaze
3 min readNov 8, 2024

--

In this article, I will list some JavaScript Interview Questions and answers to help you prepare for your next Job!

1. How do you sort an array of numbers in ascending order in JavaScript?

const numbers = [5, 3, 8, 1, 2];

// Sorting in ascending order
numbers.sort((a, b) => a - b);

console.log(numbers); // Output: [1, 2, 3, 5, 8]

2. How do you add an element at the beginning of an array? How do you add one at the end?

// Adding an element at the beginning of an array
const a = [2, 3, 4];
const element = 1;
a.unshift(element);

console.log(a); // Output: [1, 2, 3, 4]

// Adding an element at the end of an array
const b = [1, 2, 3];
const element = 4;
b.push(element);

console.log(b); // Output: [1, 2, 3, 4]

3. How do you shuffle an array in a random order in JavaScript?

let arr = [1, 2, 3, 4, 5];
arr.sort(() => Math.random() - 0.5);
console.log(arr);

4. How to filter out falsy values from an array in JavaScript?

const array = [0, 1, false, 2, '', 3, null, 4, undefined, 5, NaN];
const filteredArray = array.filter(Boolean);
console.log(filteredArray);
// Output: [1, 2, 3, 4, 5]

5. How to remove all duplicate items from an array in JavaScript?

function removeDuplicates(arr) {
return [...new Set(arr)];
}

// Example usage
const numbers = [1, 2, 3, 2, 4, 1, 5, 6, 3];
const uniqueNumbers = removeDuplicates(numbers);
console.log(uniqueNumbers);
// Output: [1, 2, 3, 4, 5, 6]

6. How to flatten an array in JavaScript?

const arr = [1, [2, [3, [4]], 5]];

// Default flatten (depth 1)
console.log(arr.flat()); // [1, 2, [3, [4]], 5]

// Flatten to infinite depth
console.log(arr.flat(Infinity)); // [1, 2, 3, 4, 5]

// Flatten to depth 2
console.log(arr.flat(2)); // [1, 2, 3, [4], 5]

7. How to remove an element from an array in JavaScript?

// Given an array
let arr = [1, 2, 3, 4, 5];

// Remove the element '3' from the array
let newArr = arr.filter(function(value) {
return value !== 3;
});

console.log(newArr);
// Output: [1, 2, 4, 5]

8. How to find the intersection of two arrays in JavaScript?

function intersection(arr1, arr2) {
// Convert one of the arrays into a Set to allow efficient lookup
const set1 = new Set(arr1);

// Use the filter method to find common elements between the two arrays
return arr2.filter(num => set1.has(num));
}

// Test the function
const arr1 = [1, 2, 3, 4];
const arr2 = [2, 4, 6, 8];
console.log(intersection(arr1, arr2)); // Output: [2, 4]

9. How to merge two or more arrays in JavaScript?

let arr1 = ['red','yellow'];
let arr2 = ['blue','green'];
let mergedArr = [...arr1, ...arr2];

console.log(mergedArr);
// Output: ['red','yellow','blue','green']

10. How do you auto-fill an array with default values in JavaScript?

const length = 5;
const filledArray = [];
for(let i = 0; i < length; i++) {
filledArray.push('CodeyMaze');
}
console.log(filledArray);

For more questions on JavaScript, check out the link — https://codeymaze.com/category/javascript-challenges/

--

--

CodeyMaze
CodeyMaze

Written by CodeyMaze

Crafting Solutions Through Code & Words https://codeymaze.com Feel free to follow me :)

No responses yet