Skip to content

3. Arrays and Array Methods

Arrays are one of the most important topics in JavaScript interviews.

Most React and frontend interview questions use array methods for data manipulation.


What is an Array?

An array is used to store multiple values in a single variable.

Example

js
const fruits = ["Apple", "Banana", "Mango"];

Access Array Elements

js
console.log(fruits[0]); // Apple
console.log(fruits[1]); // Banana

Array Properties

length

js
const nums = [1, 2, 3];

console.log(nums.length);

Output:

js
3;

Important Array Methods


1. map() (VERY IMPORTANT)

map() creates a new array by transforming each element.


Syntax

js
array.map((item) => {
  return item;
});

Example

js
const nums = [1, 2, 3];

const doubled = nums.map((num) => num * 2);

console.log(doubled);

Output

js
[2, 4, 6];

Explanation

  • Loops through every element
  • Modifies/transforms data
  • Returns a NEW array
  • Original array remains unchanged

Real Interview Example

js
const users = [{ name: "Dipak" }, { name: "Rahul" }];

const names = users.map((user) => user.name);

console.log(names);

Output:

js
["Dipak", "Rahul"];

2. forEach()

Used for looping through arrays.


Example

js
const nums = [1, 2, 3];

nums.forEach((num) => {
  console.log(num);
});

Important Point

forEach() does NOT return anything.

js
const result = nums.forEach((num) => num * 2);

console.log(result);

Output:

js
undefined;

Difference Between map() and forEach()

Featuremap()forEach()
Returns new arrayYesNo
Used for transformationYesNo
ChainableYesNo
Main purposeModify dataLooping

3. filter() (VERY IMPORTANT)

Used to filter elements based on condition.

Returns a new array.


Example

js
const nums = [1, 2, 3, 4, 5];

const evenNums = nums.filter((num) => num % 2 === 0);

console.log(evenNums);

Output

js
[2, 4];

Explanation

  • Checks condition for each item
  • If condition is true → item included
  • Returns new filtered array

Real Interview Example

js
const users = [
  { name: "Dipak", active: true },
  { name: "Rahul", active: false },
];

const activeUsers = users.filter((user) => user.active);

console.log(activeUsers);

4. reduce() (VERY IMPORTANT)

reduce() reduces array into a single value.

Used for:

  • Sum
  • Total
  • Counting
  • Grouping
  • Object creation

Syntax

js
array.reduce((accumulator, currentValue) => {
  return result;
}, initialValue);

Example — Sum of Array

js
const nums = [1, 2, 3, 4];

const sum = nums.reduce((acc, curr) => {
  return acc + curr;
}, 0);

console.log(sum);

Output

js
10;

Step-by-Step Working

IterationacccurrResult
1011
2123
3336
46410

Explanation

  • acc = accumulator (stores result)
  • curr = current array item
  • 0 = initial value

Real Interview Example — Total Price

js
const cart = [{ price: 100 }, { price: 200 }, { price: 300 }];

const total = cart.reduce((acc, item) => {
  return acc + item.price;
}, 0);

console.log(total);

5. find()

Returns FIRST matching element.


Example

js
const nums = [10, 20, 30, 40];

const result = nums.find((num) => num > 20);

console.log(result);

Output

js
30;

Important Point

  • Returns only first match
  • Returns undefined if nothing found

Difference Between find() and filter()

find()filter()
Returns first itemReturns array
Single valueMultiple values
Stops after first matchChecks all items

6. some()

Checks if AT LEAST ONE element satisfies condition.

Returns true or false.


Example

js
const nums = [1, 2, 3, 4];

const hasEven = nums.some((num) => num % 2 === 0);

console.log(hasEven);

Output

js
true;

7. every()

Checks if ALL elements satisfy condition.


Example

js
const nums = [2, 4, 6];

const allEven = nums.every((num) => num % 2 === 0);

console.log(allEven);

Output

js
true;

Difference Between some() and every()

some()every()
One condition trueAll conditions true
Returns booleanReturns boolean

8. sort()

Used to sort array elements.


Problem with Default Sort

js
const nums = [100, 2, 25];

nums.sort();

console.log(nums);

Output:

js
[100, 2, 25];

Because sort() converts values into strings.


Correct Numeric Sort

Ascending

js
nums.sort((a, b) => a - b);

Descending

js
nums.sort((a, b) => b - a);

9. slice()

Returns portion of array.

Does NOT modify original array.


Syntax

js
array.slice(start, end);

Example

js
const nums = [1, 2, 3, 4, 5];

const result = nums.slice(1, 4);

console.log(result);

Output

js
[2, 3, 4];

10. splice()

Adds/removes elements from array.

Modifies original array.


Syntax

js
array.splice(start, deleteCount, items);

Remove Elements

js
const nums = [1, 2, 3, 4];

nums.splice(1, 2);

console.log(nums);

Output

js
[1, 4];

Add Elements

js
const nums = [1, 2, 3];

nums.splice(1, 0, 100);

console.log(nums);

Output

js
[1, 100, 2, 3];

Difference Between slice() and splice()

slice()splice()
Does not modify original arrayModifies original array
Returns copied portionAdds/removes items
Non-destructiveDestructive

Most Asked Interview Questions


Q1. Difference Between map() and forEach()

map()forEach()
Returns new arrayReturns undefined
Used for transformationUsed for looping
ChainableNot chainable

Q2. What is reduce()?

reduce() converts array into single value.

Example

js
const nums = [1, 2, 3, 4];

const sum = nums.reduce((acc, curr) => {
  return acc + curr;
}, 0);

console.log(sum);

Used for:

  • Sum
  • Total calculation
  • Flatten arrays
  • Counting values

Q3. Difference Between find() and filter()

find()filter()
Returns first matchReturns all matches
Returns single valueReturns array

Q4. Difference Between some() and every()

some()every()
One item passesAll items must pass
Boolean resultBoolean result

Interview Tips

Most Important Methods

Focus heavily on:

  • map()
  • filter()
  • reduce()
  • find()
  • some()
  • every()

These are asked in:

  • React interviews
  • JavaScript interviews
  • Machine coding rounds
  • Frontend assignments

Best Practices

  • Use map() for transforming data
  • Use filter() for filtering
  • Use reduce() for totals/calculations
  • Avoid mutating original arrays unnecessarily
  • Prefer immutable operations in React

© 2025 DDocs · Dipak's Documentation Guide