The easiest way is using shift(). If you have an array, the shift function shifts everything to the left.

var arr = [1, 2, 3, 4]; 
var theRemovedElement = arr.shift(); // theRemovedElement == 1
console.log(arr); // [2, 3, 4]
Answer from Thalsan on Stack Overflow
🌐
W3Schools
w3schools.com › jsref › jsref_shift.asp
JavaScript Array shift() Method
❮ Previous JavaScript Array Reference Next ❯ · Shift (remove) the first element of the array: const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift(); Try it Yourself » · The shift() method returns the shifted element: const ...
Discussions

Remove button only removing first item in array
cart.splice(cart[i], 1) -> cart.splice(i,1) More on reddit.com
🌐 r/learnprogramming
6
1
September 3, 2024
Remove first item of Array
How do i remove the first item of my array · Hey @Palaniyappan, this does not seem to remove the first item of the Array. When i log the items in the array after your proposed solution, the same first item I’m trying to remove still exists · This topic was automatically closed 3 days after ... More on forum.uipath.com
🌐 forum.uipath.com
1
4
January 20, 2020
Delete first Element on Array (JS) - Questions & Answers - Unity Discussions
Hey Guys! For many people its a cheap question but i dont know how to handle this: I created a Trigger which spawns Obstacles if the player goes trough - after he enters another one this trigger stop spawning new Obstacles. So i put all the new spawned Obstacles into an array, because i want ... More on discussions.unity.com
🌐 discussions.unity.com
0
July 3, 2014
Why is removing a specific element from an array so needlessly complicated in Javascript?
You could use the .filter method. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter Use it like this: const newArray = array.filter(element => element > 6) The 'element > 6' is just a placeholder example. If your array was a series of numbers, it would only return numbers that are greater than six. If you wanted it to remove a certain word from an array of words you could switch that conditional statement out with "element !== 'word' " 'newArray' will be your array without the value you're trying to remove. 'Array' represents the current array with the value you're trying to remove More on reddit.com
🌐 r/learnprogramming
9
0
January 17, 2023
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › splice
Array.prototype.splice() - JavaScript | MDN
If deleteCount is omitted, or if its value is greater than or equal to the number of elements after the position specified by start, then all the elements from start to the end of the array will be deleted. However, if you wish to pass any itemN parameter, you should pass Infinity as deleteCount to delete all elements after start, because an explicit undefined gets converted to 0. If deleteCount is 0 or negative, no elements are removed.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › delete-the-first-element-of-array-without-using-shift-method-in-javascript
Delete the first element of array without using shift() method in JavaScript - GeeksforGeeks
August 5, 2025 - let Arr = ['Geeks', 'GFG', 'Geek', 'GeeksForGeeks']; console.log("Array: [" + Arr + "]"); function removeFirst(element, index) { return index > 0; } function myGFG() { Arr = Arr.filter(removeFirst); console.log("Elements of array = [" + Arr ...
🌐
CoreUI
coreui.io › answers › how-to-remove-the-first-item-from-an-array-in-javascript
How to remove the first item from an array in JavaScript · CoreUI
September 18, 2025 - From my expertise, the most efficient and built-in solution is using the shift() method, which removes and returns the first element. This approach is clean, performant, and specifically designed for this exact use case.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › shift
Array.prototype.shift() - JavaScript | MDN
The shift() method of Array instances removes the first element from an array and returns that removed element. This method changes the length of the array.
Find elsewhere
🌐
Sentry
sentry.io › sentry answers › javascript › how can i remove a specific item from an array?
How Can I Remove a Specific Item from an Array? | Sentry
If you want to remove an item from an array, you can use the pop() method to remove the last element or the shift() method to remove the first element.
🌐
Reddit
reddit.com › r/learnprogramming › remove button only removing first item in array
r/learnprogramming on Reddit: Remove button only removing first item in array
September 3, 2024 -

Hello I've been stuck on this for quite awhile so I thought I would ask here for a bit of help.

I am currently making a shopping cart where you can add items to a basket and when going to the basket page have all the items that you have chosen be displayed in a list, I've only limited myself to using HTML, CSS and JavaScript for this project, I am also using session storage to store the products that was selected so the products can be rendered in the basket page.

The problem is I added a delete button for each of the product on the basket page, however when I click on the delete button, it only deletes the first item in my array, how do I get the delete button to delete the product that it was next to?

Here is my code for when adding the products to the basket

let itemList;

if (sessionStorage.getItem("items")) {
    itemList = JSON.parse(sessionStorage.getItem("items"))
} else {
    itemList = []
    sessionStorage.setItem("items", JSON.stringify(itemList))
}

//cupcake item
let item1 = { id:1 ,name: "cupcake", image: "images/cupcake.jpeg", price: "5.99", quantity: 1 }

//dougnut
let item2 = {id:2,name:"doughnut",price:"3.99",quantity:1}

//add to cart cupcake
document.getElementById("btnsave").addEventListener("click", function () {
    let array = JSON.parse(sessionStorage.getItem("items"))
    if(array.find((e)=> e.name === "cupcake")){
    for(let item of array) {
        if(item.name === "cupcake"){
        item.quantity += 1 
        return sessionStorage.setItem("items",JSON.stringify(array))
        }
        }
    }
    array.push(item1)
    sessionStorage.setItem("items",JSON.stringify(array))
})

//add to cart doughnut
document.getElementById("btnsave2").addEventListener("click", function () {
    let array = JSON.parse(sessionStorage.getItem("items"))
    if(array.find((e)=> e.name === "doughnut")){
    for(let item of array) {
        if(item.name === "doughnut"){
        item.quantity += 1 
        return sessionStorage.setItem("items",JSON.stringify(array))
        }
        }
    }
    array.push(item2)
    sessionStorage.setItem("items",JSON.stringify(array))    
})  

Here is my code for the basket page

let cart = JSON.parse(sessionStorage.getItem("items"))


function generateBasket() {
for (let i = 0; i < cart.length; i++) {
    let listItem = document.createElement("li")
    let image = document.createElement("img")
    let container = document.createElement("span")
    let name = document.createElement("span")
    let price = document.createElement("span")
    let quantity = document.createElement("span")
    let deleteBtn = document.createElement("button")
    deleteBtn.innerHTML = "Delete item"
    deleteBtn.addEventListener("click", function () {
      listItem.remove()
      cart.splice(cart[i], 1)
      console.log(cart)
      generateTotal()       
    })
    quantity.innerHTML = cart[i].quantity
    name.innerHTML = cart[i].name
    price.innerHTML = cart[i].price
    image.src = cart[i].image
    container.appendChild(image)
    listItem.appendChild(container)
    listItem.appendChild(name)
    listItem.appendChild(price)
    listItem.appendChild(quantity)
    listItem.appendChild(deleteBtn)
    listItem.setAttribute("class","myStyle")
    image.setAttribute("class","cupcake")
    shoppingCart.appendChild(listItem)
  }
}


function generateTotal() {
let totalArr = []
for (let product of cart) {
    let priceQuantity = product.price * product.quantity
    let rounded = priceQuantity.toFixed(2)
    let total = parseFloat(rounded)
    totalArr.push(total)
}
let totalPrice = 0
for (let i = 0; i < totalArr.length; i++) {
 totalPrice += totalArr[i]
}
  document.getElementById("total-price").innerHTML = `Your total is £${totalPrice}`
}

generateTotal()
generateBasket()
🌐
Medium
medium.com › @erictongs › the-best-way-to-remove-the-first-element-of-an-array-in-javascript-shift-vs-splice-694378a7b416
The best way to remove the first element of an array in Javascript — shift() vs splice() | by Eric Tong | Medium
March 29, 2019 - In Javascript, there are two methods in Array.prototype for removing the first element of an array: shift() and splice(). shift() doesn’t take any arguments. It returns the first element of the array and removes the element from the array.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › shift
JavaScript Array shift() - Remove First Element | Vultr Docs
November 25, 2024 - Validate the changes to the array. ... let fruits = ['apple', 'banana', 'cherry']; let removedElement = fruits.shift(); console.log(removedElement); // Outputs: apple console.log(fruits); // Outputs: ['banana', 'cherry'] Explain Code · This code demonstrates removing the first element from the fruits array.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › array › remove first or last n array elements
Remove the first or last n elements from a JavaScript array - 30 seconds of code
December 24, 2023 - In order to remove n elements from the beginning of an array, you can use Array.prototype.slice() with a positive start index and no end index. This will return a new array with the first n elements removed.
🌐
W3Schools
w3schools.com › js › js_array_methods.asp
JavaScript Array Methods
When you work with arrays, it is easy to remove elements and add new elements.
🌐
Unity
discussions.unity.com › questions & answers
Delete first Element on Array (JS) - Questions & Answers - Unity Discussions
July 3, 2014 - Hey Guys! For many people its a cheap question but i dont know how to handle this: I created a Trigger which spawns Obstacles if the player goes trough - after he enters another one this trigger stop spawning new Obstacles. So i put all the new spawned Obstacles into an array, because i want to delete them after X time (because of memory overload).
🌐
DevGenius
blog.devgenius.io › 4-ways-to-remove-the-first-x-items-from-an-array-in-javascript-c1bb907d79e3
4 Ways to Remove the First X Items from an Array in JavaScript | by Tajammal Maqbool | Dev Genius
December 3, 2024 - Since splice() modifies the original array, you no longer have the first X elements in array. The filter() method creates a new array with all elements that pass the test implemented by the provided function. It’s generally used for filtering out elements based on conditions, but it can also be used to remove the first X items.
🌐
DEV Community
dev.to › jsdevspace › 9-ways-to-remove-elements-from-arrays-in-javascript-4be6
9 Ways to Remove Elements from Arrays in JavaScript - DEV Community
August 6, 2024 - filter is then used to remove all undefined values from the resulting array. The flatMap() method first maps each element using a mapping function, then flattens the result into a new array.
🌐
CoreUI
coreui.io › blog › how-to-remove-element-from-javascript-array
How to Remove Elements from a JavaScript Array · CoreUI
February 13, 2025 - For instance, if you must remove an element of the array with a specified value, consider filter. If you just want to remove the first element or remove the last element quickly, reach for shift or pop.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Array methods
The arr.splice method is a Swiss army knife for arrays. It can do everything: insert, remove and replace elements. ... It modifies arr starting from the index start: removes deleteCount elements and then inserts elem1, ..., elemN at their place.