J

JavaScript Handbook

Clean • Professional

JavaScript Arrays — Top Interview Questions & Answers

3 minute

JavaScript Arrays — Interview Questions & Answers

Ques: What is an Array in JavaScript?

Ans: An Array in JavaScript is a special type of object used to store multiple values in a single variable. Each value is stored at a numeric index, starting from 0.

Example:

let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // Apple

Ques: How do you create an Array in JavaScript?

Ans: There are two ways to create arrays:

Array Literal (Recommended):

let colors = ["Red", "Green", "Blue"];

Array Constructor:

let numbers = new Array(1, 2, 3, 4);

Ques: What are Array Indexes in JavaScript?

Ans: Each element in an array has an index that starts at 0. You can access, modify, or delete elements using these indexes.

Example:

let cars = ["BMW", "Audi", "Tesla"];
cars[1] = "Mercedes"; // modifies element
console.log(cars[1]); // Mercedes

Ques: How can you find the length of an array?

Ans: Use the .length property.

Example:

let cities = ["Delhi", "London", "Paris"];
console.log(cities.length); // 3

Ques: What are common Array methods in JavaScript?

MethodDescriptionExample
push()Adds element to endarr.push("x")
pop()Removes last elementarr.pop()
unshift()Adds element to startarr.unshift("x")
shift()Removes first elementarr.shift()
concat()Merges arraysarr1.concat(arr2)
join()Converts to stringarr.join(",")
slice()Extracts a sectionarr.slice(1,3)
splice()Adds/removes elementsarr.splice(2, 1, "new")

Ques: How can you search elements in an array?

Ans: You can search using:

  • indexOf() → Returns index of first match
  • lastIndexOf() → Returns index of last match
  • includes() → Returns true or false

Example:

let nums = [10, 20, 30, 40];
console.log(nums.includes(20)); // true

Ques: What are functional array methods?

  • map() → Creates new array from each element
  • filter() → Returns elements that pass a test
  • reduce() → Reduces array to a single value

Example:

let nums = [1, 2, 3, 4];
let doubled = nums.map(n => n * 2);

Ques: How do you iterate over arrays?

Ans: There are several ways to loop through an array:

let arr = ["a", "b", "c"];

for (let i = 0; i < arr.length; i++) console.log(arr[i]);

arr.forEach(item => console.log(item));

for (let item of arr) console.log(item);

Ques: How do you sort and reverse arrays?

Ans: Use .sort() for sorting and .reverse() for reversing.

Example:

let numbers = [40, 10, 30, 20];
numbers.sort((a, b) => a - b); // Ascending
numbers.reverse(); // Descending

Ques: What are Array Iteration Methods?

MethodPurpose
forEach()Executes a function for each element
map()Returns a new array with transformed values
filter()Returns elements that match a condition
reduce()Accumulates values into one result
some()Returns true if any element matches
every()Returns true if all elements match
find()Returns first matching element
findIndex()Returns index of first match

Ques: What is the difference between map() and forEach()?

Featuremap()forEach()
Returns new array?YesNo
Use caseTransform dataIterate for side-effects

Example:

let arr = [1, 2, 3];
let doubled = arr.map(x => x * 2);
console.log(doubled); // [2,4,6]

Ques: What is an Array Reference in JavaScript?

Ans: Arrays are reference types, meaning they store a reference (address), not the actual value.

let a = [1, 2, 3];
let b = a;
b.push(4);
console.log(a); // [1,2,3,4] - both point to same reference

Ques: How do you copy an array properly?

Ans: Use spread operator or slice() for shallow copies.

let a = [1, 2, 3];
let b = [...a]; // or a.slice()

Ques: What are Typed Arrays in JavaScript?

Ans: Typed Arrays are special array-like objects that handle binary data efficiently. They are used in performance-heavy tasks like WebGL, audio, and video processing.

Example:

let buffer = new ArrayBuffer(8);
let view = new Uint8Array(buffer);
view[0] = 255;
console.log(view); // Uint8Array(8) [255, 0, 0, 0, 0, 0, 0, 0]

Ques: What are the advantages of using Arrays?

  • Store multiple values in one variable
  • Dynamic resizing
  • Fast iteration
  • Supports built-in higher-order methods (map, filter, etc.)
  • Compatible with modern JS features like destructuring and spread

Article 0 of 0