How to use shift() Method In Javascript
This method is used to delete an element from the beginning of an array. This method is used to return the first element of an array. It also decreases the length of the original array.
Example: In this example, the shift() method is used for deleting the first element of an array.
Javascript
function myFunc() { let arr = [ 'gfg' , 'GFG' , 'g' , 'w3wiki' ]; let name = arr.shift(); console.log(name); console.log(arr.length) } myFunc(); |
gfg 3
Different ways to delete an item from an array using JavaScript
In Javascript, we do not have any array.remove() method for deleting the element. we will have an array and we need to delete a given item from that array and return the resulting array in the console.
These are the following methods for solving this problem:
Table of Content
- Using for loop and push() Method
- Using Pop() Method
- Using shift() Method
- Using splice() Method
- Using filter() Method
- Using delete Operator
- Using Lodash _.remove() Method
Note: There are some other methods that are created by JavaScript inbuilt methods.