Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript - Delete an Item From an Array
Next article icon

JavaScript - Delete an Item From an Array

Last Updated : 07 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 JavaScript

1. 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) 

Output
GeeksforGeeks 3 

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) 

Output
gfg 3 

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)); 

Output
[ 2, 7, 19 ] 

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.

Next Article
JavaScript - Delete an Item From an Array

M

mansigeekso9ii
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-array
  • JavaScript-DSA
  • JavaScript-Questions

Similar Reads

    JavaScript - How to Remove an Element from an Array?
    Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po
    3 min read
    JavaScript - Delete Elements from an Index in JS Array
    These are the following ways to delete elements from a specified Index of JavaScript arrays:1. Using splice() MethodThe splice() method is used to remove elements directly from a specified index in an array by specifying the index and the number of elements to delete.JavaScriptlet a = [1, 2, 3, 4, 5
    1 min read
    How to delete an element from an array using JavaScript ?
    The array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index. In this article, we will discuss different ways to remove elements from the array.
    5 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
    Delete from a JS Array
    The following are different delete operations on a JS Array:Delete from the Beginning of JS ArrayThis 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
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences