Skip to content

WebDevHubs

  • Home
  • JavaScript
  • Toggle search form

Remove the First Element of an Array in JavaScript

Posted on December 17, 2024 By Admin No Comments on Remove the First Element of an Array in JavaScript

There are various methods to remove the first element of an array in JavaScript.

1. Using shift() Method

The shift() method removes the first element of an array. It modifies the original array by removing the first element and returns the removed element.

let arr = [10, 20, 30, 40, 50];
let item = arr.shift();

console.log(arr); // Output: [20, 30, 40, 50]
console.log(item); // Output: 10

2. Using slice() Method

The slice() method creates a new array that excludes the first element. It does not modify the original array.

let arr = [10, 20, 30, 40, 50];
let newArr = arr.slice(1);

console.log(newArr); // Output: [20, 30, 40, 50]
console.log(arr);    // Output: [10, 20, 30, 40, 50]

3. Using splice() Method

The splice() method can be used to remove elements from an array by specifying the starting index and the number of elements to remove. This method modifies the original array.

let arr = [10, 20, 30, 40, 50];
let item = arr.splice(0, 1);

console.log(arr);  // Output: [20, 30, 40, 50]
console.log(item); // Output: [10]

4. Using Destructuring Assignment

Destructuring can be used to extract the first element while keeping the rest of the array intact. This method creates a new array without modifying the original.

let arr = [10, 20, 30, 40, 50];
let [item, ...newArr] = arr;

console.log(newArr);  // Output: [20, 30, 40, 50]
console.log(item);    // Output: 10

5. Using filter() Method

The filter() method can be used to create a new array by excluding the first element based on its index.

let arr = [10, 20, 30, 40, 50];
let newArr = arr.filter((_, index) => index !== 0);

console.log(newArr); // Output: [20, 30, 40, 50]
console.log(arr);    // Output: [10, 20, 30, 40, 50]

6. Using a for Loop

For scenarios where you want full control, you can use a for loop to manually shift elements to the left, effectively removing the first element.

let arr = [10, 20, 30, 40, 50];
let newArr = [];

for (let i = 1; i < arr.length; i++) {
    newArr.push(arr[i]);
}

console.log(newArr); // Output: [20, 30, 40, 50]

7. Using Array.from() Method

You can use Array.from() to create a new array starting from the second element.

let arr = [10, 20, 30, 40, 50];
let newArr = Array.from(arr.slice(1));

console.log(newArr); // Output: [20, 30, 40, 50]
console.log(arr);    // Output: [10, 20, 30, 40, 50]
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Questions

Post navigation

Previous Post: Remove Element from Specific Position in JavaScript
Next Post: How to Print Array Elements in JavaScript?

More Related Articles

How Can I Remove a Specific Item from an Array in JavaScript? JavaScript
JavaScript Array with() Method JavaScript
How to Create an Array in JavaScript? JavaScript
How to Create Two-Dimensional Array in JavaScript? JavaScript
Array in JavaScript JavaScript
How to Center Text in HTML? CSS

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Uncategorized
  • Web Technologies
  • Web Templates

Recent Posts

  • How to Center Text in HTML?
  • Design a Simple HTML Page | First HTML Project
  • Best Way to Initialize an Empty Array in PHP
  • HTML Description Lists
  • HTML Ordered Lists

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme