Javatpoint Logo
Javatpoint Logo

TypeScript Arrays

An array is a homogenous collection of similar type of elements that have a contiguous memory location.

An array is a user-defined data type.

An array is an object which has a special type of data structure where we store similar elements. In an array, we can store only a fixed set of elements.

The array is index-based storage, the first element of an array is stored at index 0. The below structure helps to understand the structure of an array.

TypeScript Arrays

Characteristics of an Array

  1. An array stored elements that have the same data type.
  2. Array elements are stored in contiguous memory locations.
  3. 2-D array elements are stored row by row in a contiguous memory location.
  4. Array name represents the address of the starting element.
  5. The size of an array should be mentioned in the declaration time.
  6. Array size should be a constant expression and not a variable.
  7. The elements of an array can be retrieved by specifying the element's corresponding index value.

Advantage

Code Optimization: An array help to make the code optimized, we can retrieve or sort the array data efficiently.

Random access: In an array, we can get any data located at an index position.

Disadvantage

Size Limit: An array can store only the fixed size of elements. We cannot grow its size at runtime.

Array declaration

Just like JavaScript, TypeScript also supports arrays. There are two ways to declare an array:

1. Using square brackets.

2. Using a generic array type.

Types of array in TypeScript

There are two types of an array:

  1. Single-Dimensional Array
  2. Multi-Dimensional Array

Single-Dimensional Array

A single-dimensional array is the simplest form of an array that contains only one row for storing data. It has a single set of the square bracket ("[]").

Syntax:

Initialization:

Example:

Output:

Array[0]: 1
Array[1]: 2

Multi-Dimensional Array

In the multi-dimensional array, data is stored in a row and column based index (also known as matrix form). A two-dimensional array is the simplest form of a multi-dimensional array.

TypeScript Arrays

Syntax:

Initialization:

Example:

Output:

1
2
3

5
6
7

Array Object

We can create an array by using the Array object. The Array constructor is used to pass:

  • A numeric value which represents the size of an array or
  • A list of comma separated values.

Syntax:

Example:

Output:

JavaTpoint
2200
Java
Abhishek

Array Traversal by using for...in loop

Example:

Output:

JavaTpoint
2300
Java
Abhishek

Passing Arrays to Functions

We can pass arrays to functions by specifying the array name without an index.

Example:

Output:

JavaTpoint
2300
Java
Abhishek

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.

Example:

Output:

CopiedArray: 1,2,3
NewArray: 1,2,3,7,8
MergedArray: 1,2,3,4,5,6

Array Methods

The list of array methods with their description is given below.

SN Method Description
1. concat() It is used to joins two arrays and returns the combined result.
2. copyWithin() It copies a sequence of an element within the array.
3. every() It returns true if every element in the array satisfies the provided testing function.
4. fill() It fills an array with a static value from the specified start to end index.
5. indexOf() It returns the index of the matching element in the array, otherwise -1.
6. includes() It is used to check whether the array contains a certain element or not.
7. Join() It is used to joins all elements of an array into a string.
8. lastIndexOf() It returns the last index of an element in the array.
9. Pop() It is used to removes the last elements of the array.
10. Push() It is used to add new elements to the array.
11. reverse() It is used to reverse the order of an element in the array.
12. Shift() It is used to removes and returns the first element of an array.
13. slice() It returns the section fo an array in the new array.
14. sort() It is used to sort the elements of an array.
15. splice() It is used to add or remove the elements from an array.
16. toString() It returns the string representation of an array.
17. unshift() It is used to add one or more elements to the beginning of an array.
Next TopicTypeScript Tuples




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA