Skip to content

Iterators and for...of

Goal: Loop over arrays and iterable values.

Standard for...of works on arrays and other iterables.

Example

function sum(nums: number[]): number {
  let total: number = 0
  for (const n of nums) {
    total = total + n
  }
  return total
}

const values: number[] = [1, 2, 3]
console.log(sum(values))

Open in playground

Key takeaways

  • Loop variables are inferred from the iterable element type.
  • Use @superjs/std-collections List for functional helpers.
  • Generators follow ECMAScript rules.

Next: Serverless handlers

Documentation