➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
JavaScript Get First Element of Array | Similar to arrays in other programming languages, the Array object permits the storage of a collection of various elements under a single variable name and provides members for carrying out standard array operations. JavaScript arrays can hold a variety of data types and are resizable. (If you’d rather not have those features, use typed arrays.)
Since JavaScript arrays are not linked arrays, it is not possible to access array items using random strings as indexes; instead, nonnegative integers (or their corresponding string forms) must be used instead.
JavaScript arrays are zero-indexed, with the first element at index 0, the second at index 1, the third at index 2, and so on, with the final member at the array’s length property’s value minus 1.
This article will teach you how to retrieve the first element in an array in JavaScript and walk you through how it’s done using examples.
JavaScript Get First Element of Array Using [ ] Operator
Using the [ ] operator is a quick and rather effective way to get the first element of an array in JavaScript. Below is an example of this technique:
var arr = [1, 2, 3, 4, 5];
var firstElement = arr[0];
console.log("First element of the array is: ", firstElement);
Output:-
First element of the array is: 1
Get First Element Of Array in JavaScript Using Array.prototype.shift()
While returning the first element from an array, the shift() method also eliminates the element from the array. You can make a copy of the array before using the shift() method to avoid making changes to the original array. There are two ways to do this:-
1. Slicing
var arr = [1, 2, 3, 4, 5];
var firstElement = arr.slice(0, 1).shift();
console.log("First element: ", firstElement);
Output:-
First element: 1
2. ES6 Spread Operator
var arr = [1, 2, 3, 4, 5];
var first = [...arr].shift();
console.log("First element: ", first);
Output:-
First element: 1
The above method for huge arrays is prohibitive.
Get First Element In Array in Javascript Employing jQuery
When using jQuery, you may get the first element by passing index 0 to the.get(index) function.
const { JSDOM } = require("jsdom");
const { window } = new JSDOM();
var $ = require("jquery")(window);
var arr = [1, 2, 3, 4, 5];
var first = $(arr).get(0);
console.log("First element: ", first);
Output:-
First element: 1
JS Get First Element Of Array Using the Assignment Destructuring
As an alternative, you can obtain the array’s initial element using the Destructuring Assignment syntax.
var arr = [10, 20, 30, 40, 50];
const [first] = arr;
console.log("First element: ", first);
Output:-
First element: 10
JavaScript Get First Item In Array Using the Lodash/Underscore Library
As an alternative, you can use the _.first method of the Underscore JavaScript library, which, if no arguments are provided, simply returns the first entry of an array. You can also use its aliases _.head and _.take.
var _ = require('underscore');
var arr = [1, 2, 3, 4, 5];
var first = _.first(arr);
console.log("First element: ", first);
Output:-
First element: 1
The _.head method in Lodash also retrieves the first entry of an array. You can also use its alias, _.first.
var _ = require('lodash');
var arr = [1, 2, 3, 4, 5];
var first = _.head(arr);
console.log("First element: ", first);
Output:-
First element: 1
This brings us to the end of the article, we hope you now fully understand how to get rid of the first element in the array in JavaScript.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!