Skip to content

Instantly share code, notes, and snippets.

@kirilkirkov
Created August 28, 2022 07:25
Show Gist options
  • Save kirilkirkov/fc6e20f9cee38031753d3828386f23f8 to your computer and use it in GitHub Desktop.
Save kirilkirkov/fc6e20f9cee38031753d3828386f23f8 to your computer and use it in GitHub Desktop.
JavaScript - Destructuring To Get The First And Last Element Of An Array In ES6
The easiest lay to get first and last element from an array in javascript with one line code.
<script>
const meals = ['apple', 'orange', 'bananna'];
const {length, 0: first, [length - 1]: last} = meals;
console.log(first, last)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment