Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is this variable referring to in typescript?

So what I am doing is trying to understand what const [,] or const [_,,,] does in this for of loop in typescript or javascript. What are these?

function getArr() {
    return ['a','b', 'c']
}

for (const [,] of getArr()) {
}

for (const [_,,,] of getArr()) {
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In your example both for loops will cause an errors. What you want in JS is called Destructuring assignment. For example you have a function getArr() which returns an array, but you want to get (for some reason) only first array value. You can use Destrictuing Assignment.

const [first, ...rest] = getArr();
console.log(first) // Will output: 'a'
console.log(rest) // Will output: ['b', 'c']

In your example, in the first loop you’re trying to use Destructing Assignment on and variable, not an array, so error will happen. Just look how for…of loop works with arrays. Obviously in the second for..of loop you trying to do the same, but saving first array element in the variable by the name _.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading