> ## 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 If Statement

The `if` statement is a fundamental control flow construct that evaluates a conditional expression and executes a corresponding block of code only if the expression resolves to a truthy value. In TypeScript, `if` statements are deeply integrated with the compiler's control flow analysis, enabling automatic type narrowing within the execution block based on type guards.

## Syntax

```typescript theme={"dark"}
if (condition) {
    // Executes if condition is truthy
} else if (alternativeCondition) {
    // Executes if alternativeCondition is truthy
} else {
    // Executes if all preceding conditions are falsy
}
```

## Expression Evaluation

TypeScript evaluates the `condition` expression by coercing the result to a boolean. Values are categorized as either *falsy* (`false`, `0`, `-0`, `0n`, `""`, `null`, `undefined`, and `NaN`) or *truthy* (all other values).

The TypeScript compiler fully permits implicit truthiness coercion in `if` statements. When compiler options like `strictNullChecks` are enabled, the type system ensures that `null` and `undefined` are strictly accounted for. The `if` statement acts as the primary mechanism to handle these constraints, allowing developers to safely filter out nullish values from a union type through standard truthiness checks or explicit comparisons.

## Control Flow Analysis and Type Narrowing

The primary distinction of the `if` statement in TypeScript compared to standard JavaScript is its role in the static type system. When an `if` statement contains a type guard (e.g., `typeof`, `instanceof`, `in`, equality checks, or custom type predicates), the TypeScript compiler performs control flow analysis to narrow a union type to a more specific type within the lexical scope of the `if` block.

```typescript theme={"dark"}
function processValue(value: string | number | boolean) {
    if (typeof value === "string") {
        // Compiler narrows 'value' to type 'string'
        value.toUpperCase();
    } else if (typeof value === "number") {
        // Compiler narrows 'value' to type 'number'
        value.toFixed(2);
    } else {
        // Compiler infers 'value' as type 'boolean' through exhaustive checking
        value.valueOf();
    }
}
```

## Short-Circuit Evaluation and Type Safety

When evaluating compound conditions using logical operators (`&&`, `||`), the `if` statement employs short-circuit evaluation. TypeScript's type checker maps this sequential evaluation, allowing safe property access or further type narrowing on objects if guarded by a preceding condition within the exact same expression.

```typescript theme={"dark"}
function processEvent(event: unknown) {
    if (typeof event === "object" && event !== null && "type" in event) {
        // The right side of && is only evaluated and type-checked 
        // if the left side proves 'event' is a non-null object.
        // Inside this block, TypeScript narrows 'event' to an object containing a 'type' property.
    }
}
```

## Discriminated Unions

TypeScript heavily relies on `if` statements to resolve discriminated unions (algebraic data types). By checking the literal type of a common discriminant property, the compiler narrows the broader union down to the specific object interface.

```typescript theme={"dark"}
type Shape = 
    | { kind: "circle"; radius: number }
    | { kind: "square"; sideLength: number };

function processShape(shape: Shape) {
    if (shape.kind === "circle") {
        // 'shape' is narrowed to the circle interface.
        // shape.radius is accessible; shape.sideLength throws a compiler error.
    }
}
```

<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>
