Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Use for...of when you want to process each value in an array or another iterable, one at a time:
const values = [1, 2, 3];
for (const value of values) {
console.log(value);
}
The loop gets values from the iterable’s Symbol.iterator implementation. That means arrays work, as do built-in iterables such as maps, sets, strings and typed arrays. Ordinary objects are not automatically iterable.
As an Amazon Associate I earn from qualifying purchases.
How do I use a for...of loop in TypeScript?
Place the iterable after of and declare a variable that receives each value in turn. TypeScript infers the variable’s type from the iterable:
Free tools Windows power users keep installed
One-click scans. No signup required.
const names = ["Ada", "Grace"];
for (const name of names) {
console.log(name);
}
Here, name is inferred as a string. Use const when you do not reassign the loop variable; use let if you need to reassign that binding inside the loop body.
#1 Best Overall
What can for...of iterate?
for...of works with iterables: objects that provide a Symbol.iterator implementation. The TypeScript handbook lists arrays, maps, sets, strings and typed arrays among built-in iterables. A plain object with properties is not iterable just because it has keys.
For a function that should accept different iterable collections, type its input as Iterable<X>, where X is the element type:
Rank #2
- TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
- TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
function toArray<X>(xs: Iterable<X>): X[] {
return [...xs];
}
This accepts arrays and other values that implement the iterable protocol. See the TypeScript handbook’s iterator and generator documentation for the protocol and additional examples.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11What is the difference between for...of and for...in?
Use for...of for values and for...in for property names. With an array, for...in produces index-like strings, while for...of produces the array elements:
const list = [4, 5, 6];
for (const index in list) {
console.log(index); // "0", "1", "2"
}
for (const value of list) {
console.log(value); // 4, 5, 6
}
for...in also applies to objects generally. for...of requires an iterable, so it is the right choice when the code needs each collection value rather than its property name.
Can I use for...of with a Map or Set?
Yes. Maps and sets are built-in iterables, so they can be used directly after of. A map loop yields entries; a set loop yields its values:
const scores = new Map([["Ada", 10], ["Grace", 12]]);
for (const [name, score] of scores) {
console.log(name, score);
}
const tags = new Set(["typescript", "javascript"]);
for (const tag of tags) {
console.log(tag);
}
Why does for...of give an error with my TypeScript target?
The TypeScript target affects the JavaScript that the compiler emits and which iteration behavior it can support. The handbook documents that ES5-targeted output permits iterator use only on values of Array type, even if a non-array object implements Symbol.iterator. For an ES2015-compliant target, TypeScript emits for...of to use the runtime’s built-in iterator implementation. Check both the project’s compiler configuration and the JavaScript runtime where the output will run; compiler output cannot make a runtime support features it lacks. The official handbook describes these target-dependent behaviors.
What does downlevelIteration do?
The downlevelIteration compiler option was designed to make iteration features more closely emulate ES6 behavior in ES5 environments. When Symbol.iterator exists, its helper can use that implementation; without it, the helper falls back to other behavior. The option does not install Symbol.iterator or add a runtime polyfill. See the official option reference.
Best Value
Version matters when following older advice: the TypeScript 6.0 release notes deprecate both target: es5 and downlevelIteration. They recommend using an external compiler or post-processing if ES5 output remains necessary. Check the TypeScript 6.0 release notes alongside the compiler version used by your project.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




