10 examples of 'get last item in array javascript' in JavaScript

Every line of 'get last item in array javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
155export function last<A>(array: Array<A>): Optional<A> {
156 const length = array.length;
157 return length ? array[length - 1] : undefined;
158}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

13function last(array) {
14 const length = array == null ? 0 : array.length
15 return length ? array[length - 1] : undefined
16}
163export function last(array) {
164 return array[array.length - 1]
165}
65function last(array) {
66 return array[array.length - 1];
67}
78function lastItem(index) {
79 var len = sets[index].length;
80 return sets[index][len-1];
81}
158export function array_item_at<T>(array: T[], index: number): T {
159 if (index >= array.length) {
160 return array[index % array.length];
161 }
162 else if (index < 0) {
163 return array[array.length - (-index % array.length)];
164 }
165 else {
166 return array[index];
167 }
168}
51static last(arr: any) {
52 return arr[arr.length - 1];
53}
1export default function last(arr) {
2 return arr[arr.length - 1];
3}
3export default function last(arr: ?Array<any>) {
4 if (!arr || !arr.length) return;
5 return arr[arr.length - 1];
6}
73function last (arr) {
74 return arr[arr.length - 1];
75}

Related snippets