Arrays are a fundamental part of programming, and TypeScript offers many features to work with them efficiently. In this tutorial, we'll explore the basics of working with arrays in TypeScript.

Creating an Array


In TypeScript, you can create an array by declaring a variable with a type of Array or using the shorthand syntax with brackets []. The following examples show how to create an array of numbers and strings.

// using Array syntax
let numbers: Array<number> = [1, 2, 3, 4, 5];
let strings: Array<string> = ["apple", "banana", "cherry"];

// using shorthand syntax
let moreNumbers: number[] = [6, 7, 8, 9, 10];
let moreStrings: string[] = ["dog", "cat", "bird"];


Accessing Elements in an Array

To access elements in an array, you can use the index of the element you want to retrieve. In TypeScript, array indexes start at zero.

let fruits: Array<string> = ["apple", "banana", "cherry"];

console.log(fruits[0]); // output: "apple"
console.log(fruits[1]); // output: "banana"
console.log(fruits[2]); // output: "cherry"

You can also use a loop to iterate over all the elements in an array.

let numbers: Array<number> = [1, 2, 3, 4, 5];

for (let i = 0; i < numbers.length; i++) {
  console.log(numbers[i]);
}


Adding and Removing Elements in an Array

You can add elements to the end of an array using the push() method or the spread operator ....

let fruits: Array<string> = ["apple", "banana", "cherry"];

fruits.push("orange"); // add an element to the end of the array
fruits = [...fruits, "grapefruit"]; // add an element to the end of the array using the spread operator

console.log(fruits); // output: ["apple", "banana", "cherry", "orange", "grapefruit"]

To remove an element from the end of an array, you can use the pop() method.

let fruits: Array<string> = ["apple", "banana", "cherry"];

fruits.pop(); // remove the last element from the array

console.log(fruits); // output: ["apple", "banana"]

You can also remove elements from the beginning of an array using the shift() method.

let fruits: Array<string> = ["apple", "banana", "cherry"];

fruits.shift(); // remove the first element from the array

console.log(fruits); // output: ["banana", "cherry"]

To modify an element in an array, you can simply assign a new value to the index of the element you want to change.

let fruits: Array<string> = ["apple", "banana", "cherry"];

fruits[1] = "kiwi"; // change the second element of the array to "kiwi"

console.log(fruits); // output: ["apple", "kiwi", "cherry"]


Conclusion

Arrays are a crucial data structure in programming, and TypeScript offers many features to work with them efficiently. In this tutorial, we covered the basics of working with arrays in TypeScript, including creating arrays, accessing elements, adding and removing elements, and modifying elements. With this knowledge, you can start using arrays in your TypeScript projects with confidence. There are many more advanced features of arrays in TypeScript, such as array methods like map(), filter(), reduce(), and many others. By exploring these features, you can take your TypeScript skills to the next level.