console.log('last', [1, 3, 4, 5].slice(-1));
console.log('second_to_last', [1, 3, 4, 5].slice(-2));

Answer from Ryan Huang on Stack Overflow
🌐
Built In
builtin.com › articles › javascript-get-last-element-of-array
How to Get the Last Element of an Array in JavaScript | Built In
Summary: JavaScript offers multiple ways to get the last element of an array, including length - 1, slice(-1), pop(), at(-1) and more. Some methods mutate the original array, while others preserve it, making method choice dependent on use case. ...
🌐
GitHub
gist.github.com › kirilkirkov › fc6e20f9cee38031753d3828386f23f8
JavaScript - Destructuring To Get The First And Last Element Of An Array In ES6 · GitHub
JavaScript - Destructuring To Get The First And Last Element Of An Array In ES6 - gist:fc6e20f9cee38031753d3828386f23f8
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
JavaScript arrays are zero-indexed: the first element of an array is at index 0, the second is at index 1, and so on — and the last element is at the value of the array's length property minus 1.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › lastIndexOf
Array.prototype.lastIndexOf() - JavaScript | MDN
The array is searched backwards, starting at fromIndex. const animals = ["Dodo", "Tiger", "Penguin", "Dodo"]; console.log(animals.lastIndexOf("Dodo")); // Expected output: 3 console.log(animals.lastIndexOf("Tiger")); // Expected output: 1 ... Element to locate in the array.
🌐
Flexiple
flexiple.com › javascript › get-last-array-element-javascript
How to Get Last Element in an Array in JavaScript? - Flexiple
Get the last element of an array in JavaScript by using the length property of the array and subtract 1 from it.
🌐
Flexiple
flexiple.com › javascript › javascript-get-last-element-array
JavaScript: How to Get the Last Element of an Array - Flexiple
In this example, fruits.length - 1 evaluates to 2, which is the index of the last element in the array fruits. With the introduction of ECMAScript 6 (ES6), JavaScript developers can use new features that make code shorter and more readable.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › findLast
Array.prototype.findLast() - JavaScript | MDN
It calls a provided callbackFn function once for each element in an array in descending-index order, until callbackFn returns a truthy value. findLast() then returns that element and stops iterating through the array. If callbackFn never returns a truthy value, findLast() returns undefined.
🌐
Stack Abuse
stackabuse.com › javascript-get-last-element-in-list
JavaScript: Get Last Element in List
April 28, 2023 - In this tutorial, we'll take a look at how to get the last element in a JavaScript collection such as an array or list, with practical examples and comparison.
🌐
CodeGenes
codegenes.net › blog › destructuring-to-get-the-last-element-of-an-array-in-es6
How to Get the Last Element of an Array Using ES6 Destructuring (vs CoffeeScript) — codegenes.net
This throws TypeError: Destructuring assignment to non-existent property '0' because there’s no element to assign to last. To avoid this, use a default value: const arr = []; const [...rest, last = "default"] = arr; // Default value for empty arrays console.log(last); // "default" (no error thrown) CoffeeScript, a language that compiles to JavaScript, was popular pre-ES6 for its terse syntax. It offers its own idioms for array manipulation, including retrieving the last element.
🌐
JavaScript in Plain English
javascript.plainenglish.io › get-the-first-last-and-middle-elements-of-an-array-using-javascript-c050fd0cb993
Get the First, Last and Middle Elements of an Array using JavaScript | by Robert S (codeBelt) | JavaScript in Plain English
November 28, 2023 - Since the array length is dynamic; meaning different arrays will have different lengths this is why we do arrayData.length — 1 . In the example above, it is basically 4: last . ...rest — We can use the Spread Syntax to get the rest of the array elements and this will give us the “middle” items of the array. Hopefully, the examples below will give you a better idea of what's happing. If you are not familiar with ES6 JavaScript, let’s take a simpler approach on how to get the first and last elements of a collection of characters.
🌐
Reddit
reddit.com › r/javascript › the easy way to access the last javascript array element
r/javascript on Reddit: The easy way to access the last JavaScript array element
September 16, 2023 - .at(-1) for an array with three elements, the latter runs roughy twice as fast on my machine. This is a very simple benchmark though, and results will differ based on multiple factors. Changing existing JavaScript behaviour is indeed a no go, though. ... That test is only done on static array (no changes to it). I tested with the following codes: "list" as the random generated list of ten to twenty numbers · "result" as the collection of the last index of the random list
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › pop
Array.prototype.pop() - JavaScript | MDN
Array.prototype.shift() has similar behavior to pop(), but applied to the first element in an array. The pop() method is a mutating method. It changes the length and the content of this. In case you want the value of this to be the same, but return a new array with the last element removed, you can use arr.slice(0, -1) instead.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-array-exercise-4.php
JavaScript array: Get the last element of an array - w3resource
// Function to get the last n elements of an array const last = (array, n) => { // Check if the input array is null, return undefined if true if (array == null) return undefined; // Check if the value of n is null, return the last element of ...
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-last-n-elements-of-array
Get the last N elements of an Array in JavaScript | bobbyhadz
The Array.slice() method will return a new array containing the last N elements of the original array.