← #153 Escape CSS class names
#154

Pick the first and last items of an array

Topics: JavaScript

If you want to receive the first and last items of a given array, you might think of the common way as following:

const length = arr.length;
const first = arr[0];
const last = arr[length - 1];

Because an array is also an object, the length property can be accessed with the destructuring syntax:

const { length } = arr;

Also, an array item at any position can be accessed with its index. Hence, we can shorten three lines at the top with a single line:

const { length, 0: first, [length - 1]: last } = arr;

More

Fix a typo or suggest an improvement
#155 Share recommendation Visual Studio Code extensions