How to remove Javascript array element

In this post, we are going to learn How to remove Javascript array element single or multiple. We will use array built-in methods

1. shift() to Remove first element of array in JavaScript


Shift() :- remove the first element of array.We will understand with the below example

Syntax :

array.shift()

JavaScript Program

let lang = ['C#', 'C++', 'JS', 'Python', 'Go'];
//check array is not empty
if(lang.length >-1)
{
    //removing the first element
let item = lang.shift();
}
console.log('array after removing element= \n'+lang);

Output

array after removing element = C++,JS,Python,Go

2. Pop() to Remove element array last element


Array.pop() : its remove last element from an array.We will understand below example

Syntax

Array.pop()

JavaScript program to remove last element

let lang = ['C#', 'C++', 'JS', 'Python', 'Go'];
//check array is not empty
if(lang.length >-1)
{
    //removing the last ement
let item = lang.pop();
}
console.log('array after removing element= \n'+lang);

Output

array after removing element= 
C#,C++,JS,Python

3. Splice() to Remove element at any postion


The splice () method is used to remove or insert elements at a specific position in JavaScript.The splice method changes the original array by removing or adding items to it.

Syntax  :

array.splice(index,number of elements to delete)

JavaScript Program Example

let students = [ 'Rack','Tom',
 'Max'];
students.splice(0,2)
console.log(students)

Output

['Max']

4. indexof() to Search and Remove array Element


The array. find() method searches the given element in the array and returns its index. The search is performed from the start to end elements of the array. if a given element is not found it returns -1.If the elements occur multiple times, array.index() method will return the index of the first occurrence of the element.

JavaScript Program

let lang = ['C#', 'C++', 'JS', 'Python', 'Go'];

//finding the index of item to remove
const index = lang.indexOf('C++');
if(index>-1)
{
    //removing item based on index
let item = lang.splice(index, 1);
}
console.log('array after removing element= \n'+lang);

Output

array after removing element= 
C#,JS,Python,Go

5. Removing a Range of array Elements


In this example, we are using an array.indexof() to remove multiple elements from array.

JavaScript Program

let lang = ['C#', 'C++', 'JS', 'Python', 'Go'];
//finding the index of item to remove
const index = lang.indexOf('C++');
if(index>-1)
{    //removing 2 item based on start from the index
let item = lang.splice(index, 2);
}
console.log('array after removing element= \n'+lang);


Output

array after removing element= 
C#,Python,Go

6. splice() to Removing Multiple Elements


splice() method used to remove multiple elements from the array by using for loop. Let us understand with an example.

JavaScript Program

let lang = ['JS', 'C++', 'JS', 'Python', 'JS','C#']
;
for( var j = lang.length-1; j--;)
{
if ( lang[j] === 'JS') 
{
lang.splice(j, 1);
}}
console.log('array after removing element= \n'+lang);

Output

array after removing element= 
C++,Python,C#