Returns the second to last element or undefined when there are less than two elements in the given array.

Type signature

<T>(xs: T[]) => T | undefined

Examples

secondToLast([1, 2, 3, 4, 5]);
// ⇒ 4
Try in REPL
secondToLast([1]);
// ⇒ undefined
Try in REPL
secondToLast([]);
// ⇒ undefined
Try in REPL

Questions

  • How to get the second to last element of an array?

TypeScript sourceJavaScript source