how to get first and last element of array in javascript

The rest parameter can only use at the end not anywhere else in the destructuring so it won't work as you expected.

Instead, 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.

let array = [1,2,3,4,5,6,7,8,9,0]

let {0 : a ,[array.length - 1] : b} = array;
console.log(a, b)
Expand snippet
Or 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.

let {0 : a ,length : l, [l - 1] : b} = [1,2,3,4,5,6,7,8,9,0];
console.log(a, b)

3.25
4
Ten1o 120 points

                                    let {0 : a ,length : l, [l - 1] : b} = [1,2,3,4,5,6,7,8,9,0];
console.log(a, b)

3.25 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
first and last index arr js display the last element in an array the first javascript js get first and last element in array create an array in javascript using first and last value get first and last element of array js get first and last element of array javascript array take last element and put it first first and last elements of array js get the first and last value of lst in js js array fine first and last index how to make first and last in an array array first and last javascript js get first and last element of array get first and last array javascript How do you access the first and last elements of an array called x? how to get first andd last element of an array js last element of array to the first last item to first item in array javascript javascript last array element to the beginning array first and last element javascript javascript get first and last element of array js get the 3 last first element of an array get first and last 4 value in node js get first and last element array javascript js array set last item as first operate on last or first array javascript take first element of array and put it as last element how to get the last entry if array into first in javascript take out first and last item in array get first and last index for array why is last element in the array the first value why is last array value first first and last element of array get the first and the last element of an array javascript intervet first and last element from array javascript get the first element of array last javascript last element of array first es6 get first and last element of array display array from last to first element js display array from last to first element last element shoult first in array matching first and last item of array js get first last of array js get first and last object in array javascript javascript array from last to first array first and array last js array first and array last return first and last item in array javascript if last one in array get first element js js get element first to last and array where the last item becomes the first get first and last element of array javascript how to get first and last element of array in javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source