To find the last element in an array in TypeScript, you can use the square bracket notation to access the last element of the array using the index -1. Here is the code:
index.ts112 chars4 lines
In this example, we declare an array of numbers, and then access the last element of the array using the index arr.length-1
. This works because array indexes in TypeScript (and most programming languages) start at 0, and the last element of the array is always at the index arr.length-1
. We then assign this value to the variable lastElement
, and print it to the console.
gistlibby LogSnag