Array Constructor
Another way to declare an array of strings in TypeScript is by using the Array constructor. You can specify the type of elements the array will contain by passing it as an argument to the constructor.
Syntax:
let myArray: Array<string> = new Array<string>("string1", "string2", "string3");
Example: In this example, we use the Array constructor to create an array named colors containing different color names as strings.
Javascript
let colors: Array<string> = new Array<string>( "Red" , "Green" , "Blue" ); console.log(colors); |
Output
["Red", "Green", "Blue"]
How to Declare an Array of Strings in TypeScript ?
Arrays are fundamental data structures in TypeScript, enabling developers to manage collections of elements efficiently.
Below are the approaches to declare an Array of strings in TypeScript:
Table of Content
- Square Brackets Notation
- Array Constructor
Contact Us