mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-21 05:51:31 +02:00
[typescript/en] Add doc for for-of and for-in loops in Typescri… (#3548)
[typescript/en] Add doc for for-of and for-in loops in Typescript
This commit is contained in:
@@ -223,6 +223,30 @@ moreNumbers[5] = 5; // Error, elements are read-only
|
|||||||
moreNumbers.push(5); // Error, no push method (because it mutates array)
|
moreNumbers.push(5); // Error, no push method (because it mutates array)
|
||||||
moreNumbers.length = 3; // Error, length is read-only
|
moreNumbers.length = 3; // Error, length is read-only
|
||||||
numbers = moreNumbers; // Error, mutating methods are missing
|
numbers = moreNumbers; // Error, mutating methods are missing
|
||||||
|
|
||||||
|
// Iterators and Generators
|
||||||
|
|
||||||
|
// for..of statement
|
||||||
|
// iterate over the list of values on the object being iterated
|
||||||
|
let arrayOfAnyType = [1, "string", false];
|
||||||
|
for (const val of arrayOfAnyType) {
|
||||||
|
console.log(val); // 1, "string", false
|
||||||
|
}
|
||||||
|
|
||||||
|
let list = [4, 5, 6];
|
||||||
|
for (const i of list) {
|
||||||
|
console.log(i); // "4", "5", "6"
|
||||||
|
}
|
||||||
|
|
||||||
|
// for..in statement
|
||||||
|
// iterate over the list of keys on the object being iterated
|
||||||
|
for (const i in list) {
|
||||||
|
console.log(i); // "0", "1", "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Further Reading
|
## Further Reading
|
||||||
|
Reference in New Issue
Block a user