Create populated in TypeScript

Find this useful? Support us: Star on GitHub 6
Category: String | Language: TypeScript

Sure!
Arrays are a common data structure in TypeScript that allow storing multiple values in a single variable. We can create arrays in TypeScript using the following syntax:

let myArray: [] = [value1, value2, value3];

Here's an example of creating an array of numbers:

let numbers: number[] = [1, 2, 3, 4, 5];
console.log(numbers); // Output: [1, 2, 3, 4, 5]

We can also create an array of strings like this:

let fruits: string[] = ["apple", "banana", "orange"];
console.log(fruits); // Output: ["apple", "banana", "orange"]

We can create an array of mixed data types as well, like this:

let mixedArray: (string|number|boolean)[] = ["hello", 42, true];
console.log(mixedArray); // Output: ["hello", 42, true]

We can also create an empty array and add elements later like this:

let emptyArray: string[] = [];
emptyArray.push("hello");
emptyArray.push("world");
console.log(emptyArray); // Output: ["hello", "world"]

Hope that helps!