Typescript for beginners: array
Evaldas
Posted on September 15, 2020
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.
💖 💪 🙅 🚩
Evaldas
Posted on September 15, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
undefined Setting Up a Node.js Development Environment with MySQL, Sequelize, and TypeScript
November 29, 2024