Skip to content
Advertisement

Declare an array in TypeScript

I’m having trouble either declaring or using a boolean array in Typescript, not sure which is wrong. I get an undefined error. Am I supposed to use JavaScript syntax or declare a new Array object?

Which one of these is the correct way to create the array?

JavaScript

How would I initialize all the values to be false?

How would I access the values, can I access them like, columns[i] = true;..?

Advertisement

Answer

Here are the different ways in which you can create an array of booleans in typescript:

JavaScript

You can access them using the index:

JavaScript

and you add elements using push:

JavaScript

When creating the array you can supply the initial values:

JavaScript
Advertisement