In this Blog  we will learn what is an array in TypeScript , features of TypeScript .

Array :

 An array is a unique kind of data type that uses a particular syntax to sequentially store many values of various data kinds.

Arrays in type script:

TypeScript supports the following concepts in arrays 

Multidimensional arrayA multidimensional array can be used with TypeScript. The multidimensional array can be broken down into its two simplest forms, or dimensions.

Passing Arrays to  FunctionsThe array’s name, without an index, can be used to pass a pointer to the method.

 Return Array from functions :permits a function to return an array.

Features of an Array :

Here is 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 cannot 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 are 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 cannot be deleted.
Declaring and initializing an array  in Typescript:

To declare an array in typescript it uses the following syntax

Syntax :

var array_name[:datatype];        //declaration 

array_name = [val1,val2,…..,valn]   //initialization

Without specifying a data type, an array declaration is assumed to be of type any. When an array is initialized, its type can be deduced from the data type of its first element.

We can also declare  & initialize the array in single Statement.

var array_name[:datatype] = [val1,val2,…..,valn]

Note :  The pair [] is called the dimensions of the value.

Accessing  an Array in Typescript :

An array element is identified by the array name followed by the subscript.

Syntax:

array_name[subscript] = Value

Example:

var data: string[];

data = [“a”, “b”, “c”, “d”];

console.log(data[0]);

console.log(data[1]);

 
Array object in Typescript :

we can Create an array using array object in TypeScript.

An array constructer can pass a numeric value That represents a size of an array or list of comma separated  values.

Example:

var arr_data:number[] = new Array(5)  

for(var i = 0;i<arr_data.length;i++) { 

    arr_data[i] = i * 4 

   console.log(arr_data[i]) 

}

Methods of an Array in Typescript:

An array in Typescript contains some methods as shown below.

1.contact() :

Returns a new array comprised of this array joined with other array(s) and/or value(s).

2.every():

If every element in this array satisfies the specified testing function, then this function returns true.

3.filter():

Arrays are created using all of the elements from the current array whose filtering function returns true.

4.forEach():

Executes a function call for every element of the array.

5.indexOf():

Returns -1 if no element matching the provided value can be found, or the first (least) index of one.

6. join():

Combines all of an array’s elements into a string.

7.lastIndexOf():

Gives back the last (largest) index of an element with the supplied value inside the array, or -1 if none was found

8.map():

Calls a given function on each element of this array, producing a new array with the results.

9. pop():

Returns the element that was removed from the end of an array.

10.push():

Returns the new length of the array after adding one or more elements to the end.

11.reduce():

Apply a function to both of the array’s values concurrently (from left to right) to condense the array to a single value.

12.reduceRight():

To combine two array values into one, apply a function to both of them at once (from right to left).

13.reverse():

The order of the elements in an array is reversed; the first element becomes the last, and the last element becomes the first.

14.shift():

Returns the element that was removed as the first element from an array.

15.slice():

Creates a new array by extracting a portion of an existing one.

16.some():

If any element in this array satisfies the specified testing function, then this function returns true.

17.sort():

Sorts the elements of an array.

18.splice():

Removes or inserts elements into an array.

19.toString():

Returns a string containing the elements of the array.

20.unShift():

adds one or more elements to the beginning of an array and returns the array’s new length.