How to remove an element from an array? Name different ways.

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Medium
#JavaScript #Arrays #JS Basics

Brief Answer

There are several ways to remove an element from an array in JavaScript:

  1. pop() — removes last element 📉
  2. shift() — removes first element 📉
  3. splice() — removes elements by index 🔪
  4. filter() — creates new array without needed elements 🔄
const arr = [1, 2, 3, 4, 5];
 
// Remove last
arr.pop(); // [1, 2, 3, 4]
 
// Remove first
arr.shift(); // [2, 3, 4]
 
// Remove by index
arr.splice(1, 1); // [2, 4]
 
// Create new without element
const filtered = arr.filter(x => x !== 2); // [4]

Full Answer

Removing an element from an array is like taking an item out of a box. There are different tools for this! 📦🔧

pop() — removes last element

Simplest way — remove the last item from the box:

const fruits = ['apple', 'banana', 'orange'];
 
// Remove last element
const last = fruits.pop(); // Returns 'orange'
 
console.log(fruits); // ['apple', 'banana']
console.log(last);   // 'orange'

shift() — removes first element

Removes the first item from the box:

const fruits = ['apple', 'banana', 'orange'];
 
// Remove first element
const first = fruits.shift(); // Returns 'apple'
 
console.log(fruits); // ['banana', 'orange']
console.log(first);  // 'apple'

splice() — removes by index

Like a surgical scalpel — you can remove any element:

const fruits = ['apple', 'banana', 'orange', 'pear'];
 
// Remove one element at index 1
fruits.splice(1, 1); // Removes 'banana'
console.log(fruits); // ['apple', 'orange', 'pear']
 
// Remove several elements
fruits.splice(0, 2); // Removes 'apple' and 'orange'
console.log(fruits); // ['pear']

filter() — creates new array

Creates a new box without needed items:

const numbers = [1, 2, 3, 4, 5];
 
// Create new array without twos
const filtered = numbers.filter(x => x !== 2);
 
console.log(numbers); // [1, 2, 3, 4, 5] — original unchanged
console.log(filtered); // [1, 3, 4, 5] — new array

There’s also [delete], but you shouldn’t use it:

const arr = [1, 2, 3];
delete arr[1]; // Don't do this!
 
console.log(arr); // [1, empty, 3] — hole in array!

When to Use What

pop/shift — for array ends

// ✅ When you need to remove from edges
const stack = [1, 2, 3];
stack.pop();  // Remove from end — stack
stack.shift(); // Remove from start — queue

splice — for middle

// ✅ When you need to remove by index
const arr = [1, 2, 3, 4, 5];
arr.splice(2, 1); // Remove element at index 2

filter — for conditional removal

// ✅ When you need to remove by condition
const numbers = [1, 2, 3, 4, 5];
const evens = numbers.filter(x => x % 2 === 0); // [2, 4]

Common Mistakes

Using delete

// ❌ Bad — creates holes
const arr = [1, 2, 3];
delete arr[1];
console.log(arr); // [1, empty, 3]
 
// ✅ Good — use splice
const arr2 = [1, 2, 3];
arr2.splice(1, 1);
console.log(arr2); // [1, 3]

Forgetting that methods modify array

// ❌ Mistake — think filter modifies array
const arr = [1, 2, 3];
arr.filter(x => x !== 2); // Doesn't modify arr!
 
// ✅ Correct — save result
const newArr = arr.filter(x => x !== 2);

Simple Rules

  1. pop() — removes from end 📉
  2. shift() — removes from start 📉
  3. splice() — removes by index 🔪
  4. filter() — creates new array without elements 🔄
  5. delete — don’t use, creates holes ⚠️
  6. pop/shift/splice — modify original 🔄
  7. filter — creates new array 🆕

Knowing different removal methods helps choose the right tool for each task! 💪


Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪