how to get first and last element of array in javascript

Solutions on MaxInterview for how to get first and last element of array in javascript by the best coders in the world

showing results for - "how to get first and last element of array in javascript"
Julia
05 Jun 2018
1The rest parameter can only use at the end not anywhere else in the destructuring so it won't work as you expected.
2
3Instead, you can destructor certain properties(an array is also an object in JS), for example, 0 for first and index of the last element for last.
4
5let array = [1,2,3,4,5,6,7,8,9,0]
6
7let {0 : a ,[array.length - 1] : b} = array;
8console.log(a, b)
9Expand snippet
10Or its better way to extract length as an another variable and get last value based on that ( suggested by @Bergi) , it would work even there is no variable which refers the array.
11
12let {0 : a ,length : l, [l - 1] : b} = [1,2,3,4,5,6,7,8,9,0];
13console.log(a, b)
Kayna
19 Aug 2019
1let {0 : a ,length : l, [l - 1] : b} = [1,2,3,4,5,6,7,8,9,0];
2console.log(a, b)
queries leading to this page
javascript last element of array firstdisplay first and last value in array javascriptget first and last 4 value in node jsjavascript array from last to firsthow to point last element of array to first element in javascriptjs array fine first and last indexget an array from last the first element javascriptfirst and last elements of array jshow do you access the first and last elements of an array called x 3ffirst and last index arr jsarray first and array last jsjs get element first to lastprint an array from last to first javascriptfirst and last index of arrayjs get first and last element of arrayjs last element of array to the firstarray first and last javascriptget first and last element array javascriptwhy is last element in the array the first valuehow to show the first and the last element of an arrayjs array set last item as firstdisplay array from last to first elementhow to point last element of array to first element javascript intervet first and last element from arrayjs array get first and last elementhow to get first and last element of array in javascriptreturn first and last item in array javascriptget first and last in arrayget first and last object in array javascriptes6 get first and last element of arrayhow to make first and last in an arrayfirst and last in arrayand array where the last item becomes the firstonly show first and last item in arrayjavascript get first and last element of arrayget first and last value of array javascriptwhy is last array value first js get first and last element in arrayhow to print last element of array in first javascriptdisplay the last element in an array the first javascriptjavascript array take last element and put it firsttake first element of array and put it as last elementget first and last index for arrayfirst and last element of arraylast element shoult first in arrayget first last of array jstake out first and last item in array assign first and last element in array jsget first and last element of array jslast item to first item in array javascriptget first and last element of array javascriptcreate an array in javascript using first and last valuejavascript first and last element of arrayget first and last element of arrayget first and last array javascriptdisplay array from last to first element jsget the first and the last element of an arrayget first and last value of array javascript with conditionjs array get first and last itemjs get the 3 last first element of an arrayif last one in array get first element jsget the first and last value of lst in jshow to get the last entry if array into first in javascriptget an array from last the first elementhow to get first andd last element of an arrayoperate on last or first array javascriptjavascript last array element to the beginningjavascript get the first element of array last array first and array lastarray first and last element javascriptjavaascript get first and last arraytake first index to last in array javascriptmatching first and last item of array jshow to get first and last element of array in javascript