How to Get First Element of Array in Node JS?

Jul 22, 2022

.

Admin

Hello Friends,

Now, let's see an example of the get First Element of Array in Node Js Example. you will learn Node.js to get the first element of the array. I explained simply how to get the first element of an array in node.js?. We will look at the example of get the first item in the array in Node Js. follow bellow step for array get the first element get using Node Js.

In this example, I will create a simple array with the days with the name. Then I will get the first element of the array using shift() function and 0 key.

let's see below a simple example with output:

Example: 1


index.js

myArray = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

# Get First Element

firstElem = myArray[0];

console.log(firstElem);

Output:

Sun

Example: 2

index.js

myArray = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

# Get First Element

firstElem = myArray.shift();

console.log(firstElem);

Output:

Sun

I hope it can help you...

#Node JS