How to Remove First Element From Array in Node.js?

27-Aug-2022

.

Admin

How to Remove First Element From Array in Node.js?

Hi friends

This tutorial will provide an example of how to remove the first element from the array in node.js. I explained simply step-by-step nodejs remove the first element from an array. if you want to see an example of remove the first element from array node js then you are in the right place. I’m going to show you about node js remove the element from the array index.

In this tutorial, we shall learn how to remove the first element from the array in node js. I will create one example using shift(). The shift() method removes the element at the zeroth index and shifts the values at consecutive indexes down, then returns the removed value. The shift() method is a mutating method. Use the shift() method to remove the first element from an array.

Let's see below simple example with output:

Step 1: Create Node.js Project


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app

cd my-app

npm init

Step 2: Update index.js File

index.js

// create array list

const fruits = ["Banana", "Orange", "Apple", "Mango"];

// remove the first element

fruits.shift();

console.log(fruits);

Output:

[ 'Orange', 'Apple', 'Mango' ]

I hope it can help you...

#Node JS