The following are different delete operations on a JS Array:
Delete from the Beginning of JS Array
This operation removes the first element of an array, shifting all other elements to lower indices. The array's length is reduced by one. Methods like shift() can be used for this, which directly mutates the array.
JavaScript
let a = [10, 20, 30, 40];
let res = a.shift();
console.log(res);
You can delete the first element of an array using many other methods. Refer to this article for more methods.
Delete from a Given Position in a JS Array
To remove an element from a specific index, you can use a method that targets that index. This operation deletes one or more elements starting at the specified index while shifting subsequent elements to fill the gap. The most commonly used method for this is splice().
JavaScript
let a = [10, 20, 30, 40];
let idx = 2;
let res = a.splice(idx, 1);
console.log(res);
You can delete an element from a specific index using many other methods. Refer to this article for more methods.
Delete an Element from the End of a JS Array
This operation removes the last element in the array, reducing its length by one. This is typically achieved using the pop() method, which mutates the original array and returns the removed element.
JavaScript
let a = [10, 20, 30, 40];
let res = a.pop();
console.log(res);
You can delete the last element of an array using many other methods. Refer to this article for more methods.
Delete First Occurrence from a JS Array
To delete the first occurrence of a specific value, the value is located within the array (e.g., using indexOf()), and then it is removed. This operation ensures that only the first matching element is deleted while leaving other instances of the value intact.
JavaScript
let a = [10, 20, 30, 20, 40];
let x = 20;
let idx = a.indexOf(x);
if (idx !== -1) {
a.splice(idx, 1);
}
console.log(a);
You can delete the first occurrence of a specific value using many other methods. Refer to this article for more methods.
Delete last Occurrence from a JS Array
This operation targets the last occurrence of a specific value in the array. Using methods like lastIndexOf() to locate the value ensures that only the last matching element is removed, leaving earlier instances unaffected.
JavaScript
let a = [10, 20, 30, 20, 40];
let x = 20;
let idx = a.lastIndexOf(x);
if (idx !== -1) {
a.splice(idx, 1);
}
console.log(a);
You can delete the last occurrence of a specific value using many other methods. Refer to this article for more methods.
Delete all Occurrences in a JS Array
Removes all instances of a specific value by filtering the array to exclude the unwanted value.
JavaScript
let a = [10, 20, 30, 20, 40];
let x = 20;
let res = a.filter(e => e !== x);
console.log(res);
You can delete all instances of a specific value using many other methods. Refer to this article for more methods.
Similar Reads
JavaScript - Delete an Item From an Array In Javascript, we do not have any array.remove() method for deleting the element. Here are the various methods we can delete an item from an array using JavaScript.Different Ways to Delete an Item From an Array Using JavaScript1. Using for loop and push() MethodThe push() method will not mutate the
4 min read
Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple",
6 min read
How To Delete An Item From State Array in ReactJS? It is important to manage the state in ReactJS for building interactive user interfaces. Sometimes when we work with arrays, we need to remove an item from these arrays. A common use case is deleting an item from an array stored in the componentâs state. In this article, weâll explore different ways
4 min read
JavaScript- Delete from a Given Position of JS Array These are the following ways to delete an item from the given array at given position: 1. Using the splice() Method(Most efficient for in-place modifications)The splice() method is the most versatile way to remove elements from any position in an array. It allows you to specify the index where the r
2 min read
How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i
4 min read
JavaScript - Delete Elements from the End of a JS Array These are the following ways to delete elements from the end of JavaScript arrays:1. Using pop() methodThe pop() method is used to remove the last element from an array and returns the modified array.JavaScriptlet a = [1, 2, 3, 4]; // Remove last element a.pop(); console.log(a);Output[ 1, 2, 3 ] 2.
2 min read