Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

Get last item from array ts

const arr: string[] = ['a', 'b', 'c'];

V1
const last = arr.at(-1);
console.log(last); // -> "c"

V2
const last = arr[arr.length - 1];
console.log(last); // -> "c"

V3
If you don't need the array afterwards, you could use  
const last = arr.pop()
console.log(last); // > "c"

V4
const last = arr.slice(-1)[0]
console.log(last); // -> "c"
 
PREVIOUS NEXT
Tagged: #Get #item #array #ts
ADD COMMENT
Topic
Name
2+5 =