DEV Community

Cover image for Typescript for beginners: array
Evaldas
Evaldas

Posted on

Typescript for beginners: array

In typecript we have two ways to work with arrays.

First way:

  • Define our variable let cities
  • Write our type to check : string[]
  • Give value to our variable = ["London", "New York", "Dubai"]

: string[] this means that we want to check if our array contains string type values. If the array contains only numbers we write it as : number[] and so on. If our array contains mixed types we will use any[]

let cities : string[] = ["London", "New York", "Dubai"];
let years : number[] = [1, 2, 3];

Second way:

  • Define our variable let cities
  • Write our type to check : Array<string>
  • Give value to our variable = ["London", "New York", "Dubai"]
let cities : Array<string> = ["London", "New York", "Dubai"];
let years : Array<number> = [1, 2, 3];

There is no difference between one way or another apart from syntax. Some might say that using the second way is a bit more clear, but that's one man's opinion.

Top comments (0)