Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  102] [ 3]  / answers: 1 / hits: 175115  / 9 Years ago, tue, july 7, 2015, 12:00:00

Is there a notation to access the last element of an array in TypeScript? In Ruby I can say: array[-1]. Is there something similar?


More From » typescript

 Answers
17

You can access the array elements by it's index. The index for the last element in the array will be the length of the array-1 ( as indexes are zero based).



This should work.



var items: String[] = [tom, jeff, sam];

alert(items[items.length-1])


Here is a working sample.


[#65897] Saturday, July 4, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristinah

Total Points: 268
Total Questions: 113
Total Answers: 89

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
;