To access the first element of an array in TypeScript, you can use the indexing notation with square brackets and the index 0.
In this example,
numbers
with numeric values.numbers[0]
and store it in a variable named firstElement
.let numbers: number[] = [10, 20, 30, 40, 50];
let firstElement: number = numbers[0];
console.log('First element:', firstElement);
First element: 10
In this example,
fruits
with string values.fruits[0]
and store it in a variable named firstFruit
.let fruits: string[] = ['Apple', 'Banana', 'Cherry', 'Date'];
let firstFruit: string = fruits[0];
console.log('First fruit:', firstFruit);
First fruit: Apple
In this tutorial, we learned How to get First Element in Array in TypeScript language with well detailed examples.