> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript For Loop

A `for` loop in TypeScript is a control flow statement used to execute a block of code repeatedly based on a boolean condition or over an iterable data structure. TypeScript enhances standard JavaScript loops by enforcing static typing, type inference, and strict scope management on loop variables, iterators, and collections.

## Standard `for` Loop

The traditional `for` loop consists of three optional expressions: initialization, condition, and afterthought. TypeScript allows explicit type annotations within the initialization block, though it typically infers the type.

```typescript theme={"dark"}
for (let i: number = 0; i < 5; i++) {
    console.log(i);
}
```

* **Initialization (`let i: number = 0`)**: Declares the loop counter. Block-scoped to the loop.
* **Condition (`i < 5`)**: Evaluated before each iteration. Must resolve to a truthy/falsy value.
* **Afterthought (`i++`)**: Executed at the end of each iteration.

## `for...of` Loop

The `for...of` statement iterates over the values of iterable objects (such as `Array`, `Map`, `Set`, and `String`). TypeScript automatically infers the type of the loop variable based on the type of the iterable.

```typescript theme={"dark"}
const sequence: number[] = [10, 20, 30];

for (const value of sequence) {
    // 'value' is implicitly typed as 'number'
}
```

When iterating over complex types like Tuples, TypeScript infers the loop variable as a union type of the tuple's elements:

```typescript theme={"dark"}
const mixedTuple: [string, number, boolean] = ["text", 42, true];

for (const item of mixedTuple) {
    // 'item' is typed as 'string | number | boolean'
}
```

## `for...in` Loop

The `for...in` statement iterates over the enumerable string properties (keys) of an object. In TypeScript, the loop variable in a `for...in` loop is always inferred as a `string`, regardless of the object's key types, because JavaScript coerces all object keys to strings at runtime.

```typescript theme={"dark"}
const registry: Record<string, boolean> = { active: true, pending: false };

for (const key in registry) {
    // 'key' is implicitly typed as 'string'
    const value: boolean = registry[key]; 
}
```

*Note: TypeScript compiler options (like `noImplicitAny`) will enforce strict indexing rules when using the string key to access object properties within the loop body.*

## Type Narrowing in Loops

TypeScript performs control flow analysis within loop bodies. If a loop variable is a union type, you can use type guards to narrow the type for specific operations.

```typescript theme={"dark"}
const data: (string | number)[] = ["a", 1, "b", 2];

for (const element of data) {
    if (typeof element === "string") {
        // 'element' is narrowed to 'string'
        element.toUpperCase();
    } else {
        // 'element' is narrowed to 'number'
        element.toFixed(2);
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
