
Arrays are an essential part of any programming language because it holds many elements in an ordering manner. All these elements in them are accessed via an index. In JavaScript, arrays are regular objects that hold the value on the specific key, a numeric key. This article will introduce how khổng lồ get the first element of an array in JavaScript.
Bạn đang xem: How to get the first element of an array?
JavaScript provides various methods to lớn get the first element. Let’s learn those methods.
Get First Array Element Using find() in JavaScript
find() is an in-built array method provided by JavaScript, which finds the elements whose value matches the specified condition.Syntax of find() in JavaScript
Parameters
$condition: It is a mandatory parameter. The user can pass any condition related khổng lồ the array element, và it will try to lớn find out the first matching element which satisfies the condition.Return Value
Return the first value which matches with a given condition. If no condition matches, it will return undefined.Example code:const arrayElements = <5, 10, 0, 20, 45>;const firstElement = arrayElements.find(element => element != undefined);console.log(firstElement);
Get First Array Element Using an array in JavaScript
As discussed earlier, each array element has a quality index assigned khổng lồ it. The numeric key/index inside an array starts with 0. Using this index, you can retrieve the first element of an array in JavaScript.Syntax of array in JavaScript
Parameters
$index: It is a mandatory parameter that accepts only integer value that specifies the index of the element to be retrieved.Xem thêm: Bệnh Ung Thư Tai Trong - Chẩn Đoán Và Điều Trị Ung Thư Tai
Return Value
It returns the element whose index is specified.Example code:const arrayElements = <5, 10, 0, 20, 45>;const firstElement = arrayElements<0>;console.log(firstElement);
Get First Array Element Using shift() in JavaScript
It is an in-built array method provided by JavaScript, which removes the first element of an array and returns the removed element. The only problem with this array method is that it mutates/modifies the original array.Syntax of shift() in JavaScript
Return Value
It returns the first element of the array, which is removed from the original array. If an array is an empty array, it will return undefined.Example code:const arrayElements = <5, 10, 0, 20, 45>;const firstElement = arrayElements.shift(0);console.log(firstElement);console.log(arrayElements);
Get First Array Element Using filter() in JavaScript
It is a JavaScript in-built array method that filters out all the elements which satisfy the given condition. It will create a copy of the matching elements array. The sole difference between find() & filter() is that first stops the traversing as soon as the first matching element is found while later will continue till the last element of an array. It is less efficient if an array form size is larger because of the traversal property.Syntax of filter() in JavaScript
Parameters
$condition: It is a mandatory parameter. The user can pass any condition related lớn the array element, và it will try khổng lồ find out all matching element that satisfies the condition.Return Value
Return the new array containing elements that match with a given condition.Example code:const arrayElements = <5, 10, 0, 20, 45>;const firstElement = arrayElements.filter(element => element!= undefined).shift();console.log(firstElement);console.log(arrayElements);
Get First Array Element Using Destructuring Assignment in JavaScript
Destructing assignments is a very powerful syntax available in JavaScript which allows unpacking the elements of array or properties of an object lớn be geted into distinct variables.Syntax of Destructuring Assignment in JavaScript
Return Value
It returns the new variable through which you can access the value of an array or object.Example code:const arrayElements = <5, 10, 0, 20, 45>;let
Output:
5
Author: Shraddha Paghdar
Shraddha is a JavaScript nerd that utilises it for everything from experimenting lớn assisting individuals và businesses with day-to-day operations & business growth. She is a writer, chef, và computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.LinkedIn
Related Article - JavaScript Array
Check if Array Contains Value in JavaScriptCreate Array of Specific Length in JavaScriptConvert Array lớn String in JavaScriptRemove First Element From an Array in JavaScriptSearch Objects From an Array in JavaScriptConvert Arguments to lớn an Array in JavaScript