last
last
(sequence: Sequence) => Value
Return the last item in a sequence
last(['a', 'b', 'c']) // is 'c' last('abc') // is 'c' last('') // is undefinedlast(['a', 'b', 'c']) // is 'c' last('abc') // is 'c' last('') // is undefinedlast(['a', 'b', 'c']) // is 'c' last('abc') // is 'c' last('') // is undefinedlast(['a', 'b', 'c']) // is 'c' last('abc') // is 'c' last('') // is undefinedSometimes we have sorted data and only care about the last item. Below, we noticed the Harry Potter book wasn't at our library. Let's find the last person to check it out so we can follow up with them.
const harryPotterCheckouts = [ { name: 'jen', checkedOut: '2025-01-05', returned: '2025-02-02', }, { name: 'mike', checkedOut: '2025-02-06', returned: '2025-02-25', }, { name: 'luke', checkedOut: '2025-03-01', }, ] const { name } = last(harryPotterCheckouts) console.log(name) // is luketype Checkout = { name: string checkedOut: string returned?: string } const harryPotterCheckouts: Checkout[] = [ { name: 'jen', checkedOut: '2025-01-05', returned: '2025-02-02', }, { name: 'mike', checkedOut: '2025-02-06', returned: '2025-02-25', }, { name: 'luke', checkedOut: '2025-03-01', }, ] const { name } = last(harryPotterCheckouts) as Checkout console.log(name) // is lukeconst harryPotterCheckouts = [ { name: 'jen', checkedOut: '2025-01-05', returned: '2025-02-02', }, { name: 'mike', checkedOut: '2025-02-06', returned: '2025-02-25', }, { name: 'luke', checkedOut: '2025-03-01', }, ] const { name } = last(harryPotterCheckouts) console.log(name) // is luketype Checkout = { name: string checkedOut: string returned?: string } const harryPotterCheckouts: Checkout[] = [ { name: 'jen', checkedOut: '2025-01-05', returned: '2025-02-02', }, { name: 'mike', checkedOut: '2025-02-06', returned: '2025-02-25', }, { name: 'luke', checkedOut: '2025-03-01', }, ] const { name } = last( harryPotterCheckouts ) as Checkout console.log(name) // is luke