Lession - #1444 TypeScript Arrays
TypeScript introduces the concept of arrays to tackle the same. An array is a homogenous collection of values. To simplify, an array is a collection of values of the same data type. It's a stoner defined type.
How do I create a new array in TypeScript?
TypeScript Array Methods.
|push(>|| Adds new elements to the array and returns the new array length|
|sort(>|| Sorts all the elements of the array|
Features of an Array
Here's a list of the features of an array −
An array declaration allocates sequential memory blocks.
Arrays are static. This means that an array once initialized can not be resized.
Each memory block represents an array element.
Array elements are identified by a unique integer called as the subscript/ index of the element.
Like variables, arrays too, should be declared before they're used. Use the var keyword to declare an array.
Array initialization refers to populating the array elements.
Array element values can be updated or modified but can not be deleted.
Declaring and Initializing Arrays
To declare an initialize an array in Typescript use the following syntax −
= ( val1, val2, valn.>
An array declaration without the data type is deemed to be of the type any. The type of such an array is inferred from the data type of the array’s first element during initialization.
Accessing Array Elements
The array name followed by the subscript is used refer to an array element. Its syntax is as follows −
An array can also be created using the Array object. The Array constructor can be passed.
A numeric value that represents the size of the array or
A list of comma separated values.
var arr_names = new Array(4>
for (var i = 0; i < arr_names.length; i++>
arr_names[i] = i * 2;