An array is a user-defined data type. An array is a homogenous collection of similar types of elements that have a contiguous memory location and which can store multiple values of different data types.
An array is a type of data structure that stores the elements of similar data type and consider it as an object too. We can store only a fixed set of elements and can’t expand its size, once its size is declared.
The array follows Index based storage i.e)the first element of an array is stored at index 0 or at index ‘i’ and the remaining elements are stored at the location ‘i+1’.
Features of an Array
1. Using square brackets.
let array_name[:datatype] = [val1, val2, valn..]
2. Using a generic array type.
TypeScript array can contain elements of different data types, as shown below.
let array_name: Array = [val1, val2, valn..]
Example: Multi Type Array
Example: Access Array Elements
Code Optimization: We can retrieve or sort the array data more efficiently.
Random access: We can randomly access the array data using the location pointer.
Size Limit: The size of an Array is fixed i.e.)static. We cannot increase the array size once it has been declared.
There are two types of an array:
It is the simplest form of an array that contains only one row for storing data. It contains single set of the square bracket (“”).
array_name = [val1, val2, valn..]
Array: 1 Array: 2
The data is stored in rows and columns (also known as matrix form) in a Multi-dimensional array.
let arr_name:datatype = [ [a1, a2, a3], [b1, b2, b3] ];
let arr_name:datatype[initial_array_index][referenced_array_index] = [ [val1, val2, val 3], [v1, v2, v3]];
10 20 30 50 60 70
We can create an Array by using or initializing the Array Object. The Array constructor is used to pass the following arguments to create an Array:
1.let arr_name:datatype = new Array(values);
GEEKSFORGEEKS 2200 Java Abhishek
Passing an Array to a Function
We can pass an Array to a function by specifying the Array name without an index.
GEEKSFORGEEKS 2300 Java Abhishek
Using TypeScript ‘Spread’ operator
The spread operator can be used to initialize arrays and objects from another array or object. It can also be used for object destructuring. It is a part of ECMAScript 6 version.
CopiedArray: 1, 2, 3 NewArray: 1, 2, 3, 7, 8 MergedArray: 1, 2, 3, 4, 5, 6
- TypeScript Accessor
- Hello World in TypeScript
- TypeScript Tuples
- TypeScript class
- Classes in TypeScript
- TypeScript Union
- Variables in TypeScript
- TypeScript String
- TypeScript Ambients Declaration
- Identifiers and Keywords in TypeScript
- Data types in TypeScript
- PHP | Arrays
- Sorting Arrays in PHP 5
- Multidimensional arrays in PHP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.