Understanding Array Types And Methods In Typescript

coding-on-laptop.jpg

Introduction:

Typescript is a strict syntactical superset of JavaScript and extends the language with optional static typing. It is intended for large-scale application development and converted to JavaScript. It's a language created and maintained by the Microsoft Company.

In this tutorial, we will talk about what an array means, the different types of arrays we have in typescript, and the array method. Arrays in Typescript are similar to that of JavaScript.

What is an Array?

An array is a special type of variable that can hold multiple values at once. They are collections of similar data elements that are stored in adjacent memory locations. It is the most basic data structure because each data element can be accessed directly by using only its index number.

Typescript Array:

A Typescript array is an ordered list of data with similar data types. The array's first element is stored at index 0. To declare an array that holds the value of a specific type you can use the following syntax:

Typescript
Let arrayName: data type[];

For example: declaring an array of strings:

Typescript
Let Name: string[];
Name = [ "Williams", "Philip", "Daniel" ]

You can add more values to the provided array by using the push() method.

Typescript
Name.push("Samson");

Output:

Typescript
Name = [ "Williams", "Philip", "Daniel", "Samson" ]

Types Of Array:

Below are the major types of an array;

1. Single-Dimensional Array This is the most basic type of array, with only one row for storing data. Syntax:

Typescript
Let arrayName: data type[]

2. Multi-Dimensional Array:

This consist of a list of several arrays, data is stored in rows and columns, which is also called the matrix form. Syntax:

Typescript
Let arrayName: data type[][]

Array Methods In Typescript

1. Sort(): This method arranges an array of characters in sorting order.

Syntax:

array.sort()

Example:

let array = [50, 10, 20, 80, 40];
let val = array.sort();

Output:

[10, 20, 40, 50, 80];

2. Push(): This adds new characters to the array and returns the array's new length. Syntax:

array.push()

Example:

let array = [20, 40, 60, 80];

//We can add a new character at the end of the array by using the push() method

let val = array.push(100);

output:

[20, 40, 60, 80, 100];

3.indexOf(): This method returns the index of an array element. All characters of an array have an index number starting from 0.

Syntax:

array.indexOf()

Example:

let array = [100, 200, 300, 400, 500];
let val = array.indexOf(400);

Output:

3

4. Pop(): this removes the last character from an element.

Syntax:

array.pop()

Example:

let array = [Volley Ball, Basket Ball, Foot Ball, Tennis];
let val = array.pop(Tennis);

Output:

[Volley Ball, Basket Ball, Foot Ball];

5. Shift(): This removes the first character from an element.

Syntax:

array.shift()

Example:

let array =  [Volley Ball, Basket Ball, Foot Ball, Tennis];
let val = array.shift();

Output:

[Basket Ball, Foot Ball, Tennis];

6. Reverse(): This method reverses an array's order.

Syntax:

array.reverse()

Example:

let array =  [Volley Ball, Basket Ball, Foot Ball, Tennis];
let val = array.reverse();

Output:

[Tennis, Foot Ball, Basket Ball, Volley Ball];

7. Concat(): As we all understand, concatenation means merging two arrays and returning the result as a combined result.

Syntax:

array1.concat(array2);

Example:

let   Colour = [Blue, Black, Pink, Yellow];
let Games =  [Volley Ball, Basket Ball, Foot Ball, Tennis];
let val = array1.concat(array2);

Output:

[Blue, Black, Pink, Yellow, Volley Ball, Basket Ball, Foot Ball, Tennis]

Conclusion:

Hopefully, you have learned what Typescript is, we have been able to look at the different types of arrays we have in Typescript, and lastly, we talked about seven different array methods. Although we have many other arrays methods, I can assure you, that you don't need to know everything, You simply need to select the ones that work best for you.