How arrays works in TypeScript ?
Last Updated :
15 May, 2024
Array in TypeScript is just similar to an array in other programming languages. The array contains homogeneous values means a collection of multiple values with different data types. TypeScript array is also fixed size, can not be resized once created. An array is a type of data structure that stores the elements of a similar data type and considers it as an object too.
How array work?
Array allocates in sequential memory blocks. Each memory block represents an array element. Elements of an array are accessed using the index of array elements for example to access 1st element of the array we need to access it using the 0th index as an index of an array starts from 0th index. Similarly, other elements get accessed using their index locations.
How to declare array in TypeScript?
The array can be written in two ways:
Using the type of elements followed by [ ] to denote an array of that element type:
var list : number[] = [1,2,3,4,5];
Using generic array type:
var list : Array<number>=[1,2,3,4,5];

How to access array elements?
To access an element of a given array we must have to write array_name with a subscript.
Example:
JavaScript
var arr : number [ ] = [1, 2, 3, 4, 5];
// Printing data
console.log(arr[0]);
console.log(arr[1]);
console.log(arr[2]);
console.log(arr[4]);
Output:
1
2
3
5
Example: In this example the Array of numbers initialized. First and last elements displayed. Element added, removed, then array iterated to print elements.
JavaScript
let numbers: number[] = [1, 2, 3, 4, 5];
console.log("First element:", numbers[0]);
console.log("Last element:", numbers[numbers.length - 1]);
numbers.push(6);
console.log("Array after adding 6:", numbers);
numbers.pop();
console.log("Array after removing last element:", numbers);
console.log("Array elements:");
for (let num of numbers) {
console.log(num);
}
Output:
"First element:", 1
"Last element:", 5
"Array after adding 6:", [1, 2, 3, 4, 5, 6]
"Array after removing last element:", [1, 2, 3, 4, 5]
"Array elements:"
1
2
3
4
5
Similar Reads
How enums works in TypeScript ? In this article, we will try to understand all the facts which are associated with enums in TypeScript. TypeScript enum: TypeScript enums allow us to define or declare a set of named constants i.e. a collection of related values which could either be in the form of a string or number or any other da
4 min read
Hello World in TypeScript TypeScript is an open-source programming language. It is developed and maintained by Microsoft. TypeScript follows javascript syntactically but adds more features to it. It is a superset of javascript. The diagram below depicts the relationship:Typescript is purely object-oriented with features like
3 min read
How TypeScript Compilation Works? TypeScript is a superset of JavaScript that adds type safety to your code. It compiles into plain JavaScript, allowing it to run in any JavaScript environment.The TypeScript compiler (tsc) checks the code for errors and then converts it into JavaScript. During this process, all TypeScript-specific f
4 min read
How to use express in typescript ? In this article, we will see how to use Express in TypeScript. The TypeScript is a superset of JavaScript that provides type notation with the JavaScript, so we can handle our server, and very scalable for the future. Express is web framework helps to create server-side handling with the help of Nod
2 min read
How optional chaining works in TypeScript ? In this article, we will try to understand how we could perform as well as analyze the working of optional chaining in TypeScript. TypeScript Optional Chaining: TypeScript Optional Chaining is the process of searching and calling variables, methods, parameters that might be nil in existence.This par
3 min read
What are Generics in TypeScript ? In this article, we will try to understand all the facts as well as the details associated with Generics in TypeScript along with some coding examples. Generics in TypeScript: Whenever any program or code is written or executed, one major thing one always takes care of which is nothing but making re
3 min read