Menu

gitpiper

last javascript Code Snippet in 2024

arraybeginner

Last Updated: 22 July 2024

Returns the last element in an array.

  • Check if arr is truthy and has a length property.
  • Use Array.prototype.length - 1 to compute the index of the last element of the given array and return it, otherwise return undefined.
const last = arr => (arr && arr.length ? arr[arr.length - 1] : undefined);
last([1, 2, 3]); // 3 last([]); // undefined last(null); // undefined last(undefined); // undefined

javascript snippet similar to last For You in July 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️