Static typing makes it possible for the Type­Script compiler to monitor the data type of array elements. This feature helps Type­Script arrays to reduce the like­li­hood of errors in your code, allowing you to develop safer and more reliable ap­pli­ca­tions.

What are Type­Script arrays?

In Type­Script, arrays are ordered lists of values. Just like in JavaScript, you can use arrays in Type­Script to store a col­lec­tion of elements. Elements can belong to different data types, including numbers, strings, objects or other arrays. Type­Script has the advantage of sup­port­ing static typing, which means that you can specify the data type of the elements in an array. This, in turn, improves error detection during de­vel­op­ment.

A key feature of arrays is their dynamic size, which allows you to add or remove elements without having to determine the size in advance. In Type­Script, arrays are mutable by default. However, you can create immutable arrays by using array methods such as map and filter. Immutable arrays can be used to create new arrays based on existing arrays. Arrays provide a con­sis­tent structure for or­ga­niz­ing data and make it easier to filter, sort and iterate over elements.

In addition, Type­Script arrays can provide a basis for im­ple­ment­ing data struc­tures such as stacks (LIFO - Last-In-First-Out) and queues (FIFO - First-In-First-Out). They are also suitable for rep­re­sent­ing lists, tables and col­lec­tions in a variety of ap­pli­ca­tions. Because elements belonging to the same type can be easily managed, arrays are par­tic­u­lar­ly useful when pro­cess­ing data from external sources, be it APIs or databases.

What is the syntax for Type­Script arrays?

In Type­Script, arrays are declared with the keywords let, const or var followed by a variable name and a data type spec­i­fi­ca­tion. When you declare a data type, you specify which data type the elements in the array should have. This is done using a colon. The elements are placed in square brackets and separated by commas in an array ini­tial­iz­er block.

The general syntax for declaring a Type­Script array is as follows:

const variableName: datatype[] = [element1, element2, ...];
type­script
  • vari­able­Name is the name you choose for the array.
  • datatype specifies the data type of the elements in the array.
  • [element1, element2, …] are the actual elements or values that are to be stored in the array. These elements should have the data type that has been specified for the array.

Here are a few examples that help to il­lus­trate the syntax:

// Data type: Number
const numbers: number[] = [1, 2, 3, 4, 5];
// Data type: String
const numbers: string[] = ["Alice", "Bob", "Charlie"];
// Data type: Boolean
const booleans: boolean[] = [true, false];
type­script

What Type­Script array methods are there?

Type­Script array methods are extremely useful and powerful because they allow you to ef­fi­cient­ly process, transform and organize data into arrays. The following table gives you an overview of common array methods in Type­Script and how they can be used.

Methods De­scrip­tion
push() Adds one or more elements to the end of the array and returns the new length of the array.
pop() Removes the last element from the array and returns it.
unshift() Adds one or more elements to the beginning of the array and returns the new length of the array.
shift() Removes the first element from the array and returns it.
concat() Combines the current array with one or more other arrays and returns a new array. The original array remains unchanged.
join(separator) Converts the elements of the array into a string and returns it. You can decide on a separator for the elements.
slice(start, end) Creates a flat copy of the array con­sist­ing of the elements between the specified indices “start” (inclusive) and “end” (exclusive). The original array remains unchanged.
splice(start, deleteCount, element1, element2, ...) Inserts new elements at the specified position and/or removes elements from the array.
forEach(callback) Executes a provided function for each element in the array.
map(callback) Creates a new array by applying a function to each element in the array.
filter(callback) Creates a new array with all elements that pass the test im­ple­ment­ed by the specified function

Type­Script array examples

Type­Script arrays are in­dis­pens­able tools when it comes to or­ga­niz­ing and pro­cess­ing data in ap­pli­ca­tions. We’ll take a look at some of the key op­er­a­tions below.

Accessing array elements

Accessing array elements in Type­Script is a basic operation that allows you to retrieve specific elements within an array. You can access array elements using their index, which rep­re­sents their position in the array. In Type­Script, array indexes are zero-based. This means that the first element has index 0 and the second element has index 1.

let myArray: number[] = [10, 20, 30, 40, 50];
// Accessing elements using index
let firstElement: number = myArray[0]; // Output: 10
let secondElement: number = myArray[1]; // Output: 20
let thirdElement: number = myArray[2]; // Output: 30
// Assigning a new value to an array element
myArray[3] = 99; // 4th element in myArray = 99
type­script

De­struc­tur­ing arrays

With array de­struc­tur­ing, you can quickly and easily extract values from an array and assign them to a variable.

let numberArray: number[] = [1, 2, 3];
// Array destructuring
let [firstNumber, secondNumber, thirdNumber] = numberArray;
// Access values
console.log(firstNumber); // Outputs 1
console.log(secondNumber); // Outputs 2
console.log(thirdNumber); // Outputs 3
type­script

Iterating over elements in Type­Script arrays

Here is an example of how to iterate over an array in Type­Script using a for loop:

let numbers: number[] = [1, 2, 3, 4, 5];
for (let i = 0; i < numbers.length; i++) {
    console.log(numbers[i]);
}
type­script

In this example, we have the numbers array, which contains numbers. We use a for loop to iterate through the array. The loop starts at i = 0, and we increment i in each loop pass. With numbers[i] we are able to access the re­spec­tive element of the array and output it.

This is the output:

1
2
3
4
5
type­script
$1 Domain Names – Register yours today!
  • Simple reg­is­tra­tion
  • Premium TLDs at great prices
  • 24/7 personal con­sul­tant included
  • Free privacy pro­tec­tion for eligible domains
Go to Main Menu