JavaScript - Delete an Item From an Array
Last Updated :
07 Jan, 2025
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() Method
The push() method will not mutate the original array. First, you have to create an empty() array and then loop over the new array and push only those elements that you want.
JavaScript
let a1 = ['gfg', 'GFG', 'g', 'GeeksforGeeks'];
const a2 = [];
for (let i = 0; i < a1.length; i++) {
if (a1[i] !== 'GFG') {
a2.push(a1[i]);
}
}
console.log(a1);
console.log(a2);
Output[ 'gfg', 'GFG', 'g', 'GeeksforGeeks' ]
[ 'gfg', 'g', 'GeeksforGeeks' ]
In this example
- It uses a for loop to iterate through arr.
- If an element is not 'GFG', it's added to a2.
- Finally, it prints both the original arr and the modified a2.
2. Using Pop() Method
Pop() method is used to delete the last element of the array and return the deleted item as an output.
JavaScript
let a1 = ['gfg', 'GFG', 'g', 'GeeksforGeeks'];
let a2 = a1.pop();
console.log(a2);
console.log(a1.length)
In this example
arr.pop()
removes and returns the last element of the array ('GeeksforGeeks'
).- The new length of
arr
(after removal) is logged.
3. Using shift() Method
Shift() method is used to delete an element from the beginning of an array.
JavaScript
let a1 = ['gfg', 'GFG', 'g', 'GeeksforGeeks'];
let a2 = a1.shift();
console.log(a2);
console.log(a1.length)
In this example
- a1.shift() removes and returns the first element of the array ('gfg').
- The new length of arr (after removal) is logged.
4. Using splice() Method
Splice() method is used for deleting the existing element or replacing the content of the array by removing/adding a new element.
JavaScript
let a = ["apple", "banana", "grapes", "strawberry"];
const rem = a.splice(2, 2, "guava");
console.log(rem);
console.log(a.length);
console.log(a);
Output[ 'grapes', 'strawberry' ]
3
[ 'apple', 'banana', 'guava' ]
In this example
- a.splice(2, 2, "guava"): This removes 2 elements from index 2 ("grapes" and "strawberry") and adds "guava" in their place.
- The removed elements are stored in rem, and a.length reflects the new size of the array after the change.
5. Using filter() Method
filter() method returns the new array. Those array element that satisfies the condition of function is only passed on to the new array.
JavaScript
const a = [2, 7, 9, 15, 19];
function isPrime(n) {
for (let i = 2; n > i; i++) {
if (n % i === 0) {
return false;
}
}
return n > 1;
}
console.log(a.filter(isPrime));
In this example
- isPrime(n): Returns true if n is prime, otherwise false.
- a.filter(isPrime): Filters array and returns a new array containing only the prime numbers from array.
6. Using delete Operator
Delete operator is more specifically used to delete JavaScript object properties.
JavaScript
const a = [2, 7, 9, 15, 19];
delete a[3];
console.log(a);
Output[ 2, 7, 9, <1 empty item>, 19 ]
- delete arr[3]: Removes the element at index 3 (value 15), but the array length remains unchanged, and the index is left with an undefined value.
7. Using Lodash _.remove() Method
The _.remove() method is used to remove all elements from the array that predicate returns True and returns the removed elements.
JavaScript
const _ = require("lodash");
let a = [1, 2, 3, 4, 5];
let even = _.remove(a, function (n) {
return n % 2 == 0;
});
console.log('Original Array ', a);
console.log('Removed element array ', even);
Output:
Original Array [ 1, 3, 5 ]
Removed element array [ 2, 4 ]
In this example
- _.remove(a, function(n) { return n % 2 == 0; }): Removes all even numbers from a and returns the removed elements in a new array (even).
- a: The original array is modified in place.
- even: Contains the even numbers that were removed.
8. Using Lodash _.pullAt method
Using Lodash's _.pullAt method removes elements from an array at specified indices. It modifies the original array, making it efficient for targeted deletions.
JavaScript
const _ = require('lodash');
let a = ['a', 'b', 'c', 'd', 'e'];
_.pullAt(a, [1, 3]);
console.log(a);
Output:
['a', 'c', 'e']
In this example
- _.pullAt(a, [1, 3]): This removes the elements at indices 1 and 3 from the array (i.e., 'b' and 'd').
- The function modifies the original array directly, removing the specified elements.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read