how find the last number in typescript

To find the last number in an array in TypeScript, you can use the length property of the array to get the total number of elements in the array and then subtract 1 from it since indexing in arrays start from 0.

Here's some code that demonstrates this:

index.ts
let arr: number[] = [1, 2, 3, 4, 5];
let lastNum: number = arr[arr.length - 1];

console.log(lastNum); // Output: 5
116 chars
5 lines

In the above code, we first create an array of numbers and store it in the variable arr. Then, we find the last number in the array by accessing the element at the index arr.length - 1 and store it in the variable lastNum. Finally, we print out the last number in the array using console.log().

gistlibby LogSnag