Open In App

How to Create Array of String in TypeScript ?

Last Updated : 06 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

An array of strings in TypeScript is a variable that can hold multiple string values. It allows you to perform manipulation of multiple strings within a single variable.

We can declare an array of strings in TypeScript in different ways as listed below.

Using Array Literal

Using array literals is the most common and straightforward way to create arrays in TypeScript. We can directly specify the array elements within square brackets.

Syntax:

let arrayName: string[] = ["val1" , "val2" , "val3" ,....."valn" ];

Example: The below code uses the array literal to create an array of strings in TypeScript.

Javascript
let colors: string[] = ["red", "green", "blue"];
console.log(colors);

Output:

["red", "green", "blue"]

Using the Array Constructor

TypeScript provides an Array constructor that can be used to create an array of strings. You can pass string values as arguments to the constructor to initialize the array.

Syntax:

let arrayName: string[] = new Array("string1", "string2", "string3");

Example: The below code uses the array constructor to define an array of strings in TypeScript.

Javascript
let arrayName: string[] =
    new Array("GFG", "JavaScript", "GeeksforGeeks");
console.log("Using the Array Constructor: ", arrayName);

Output:

Using the Array Constructor: ["GFG", "JavaScript", "GeeksforGeeks"]

NOTE: We can use the below methods to assign values to the arrays decared using the above methods.

Using the Array’s push() Method

After declaring an empty array of strings, you can add string elements to it using the push method. It’s useful when we don’t know the initial content of the array and need to add elements dynamically.

Example: The code code implements array.push() method to add elements to already created array.

Javascript
let arr: string[] = [];
arr.push("apple");
arr.push("banana");
arr.push("orange");
console.log(arr);

Output:

["apple", "banana", "orange"]

Using the fill() method

The fill method fills all the elements of an array with a static value, from a start index to an end index. This approach is useful when we want to initialize an array with repeated string values.

Syntax:

let array_Name: string[] = new Array(length).fill(value);

Example: The below code add elements to the array using the TypeScript fill() method.

Javascript
let arrayOfStrings: string[] = new Array(5).fill("hello");
console.log("Array of strings:", arrayOfStrings);

Output:

Array of strings: ["hello", "hello", "hello", "hello", "hello"]

Using Array.from() method

The Array.from() method creates a new, shallow-copied array instance from an array-like or iterable object.

Syntax:

Array.from(arrayLike, mapFn?, thisArg?)

Parameters:

  • arrayLike: An array-like or iterable object to convert to an array.
  • mapFn: Optional. Map function to call on every element of the array.
  • thisArg: Optional. Value to use as this when executing mapFn.

Example: This TypeScript code creates an array of 5 strings, each initialized with “hello”, using `Array.from()` method and an arrow function, then logs it to the console.

JavaScript
let arrayOfStrings: string[] = Array.from({ length: 5 }, () => "hello");
console.log("Array of strings:", arrayOfStrings);

Output:

Array of strings: ["hello", "hello", "hello", "hello", "hello"]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads