> ## 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 Default Parameter

A default parameter in TypeScript allows a formal parameter to be initialized with a specific value if the caller omits the corresponding argument or explicitly passes `undefined`. It guarantees that the parameter will have a defined value within the function's execution context while simultaneously informing the TypeScript compiler about the parameter's type and optionality.

## Syntax and Type Inference

When a default value is assigned, TypeScript can automatically infer the parameter's type. Explicit type annotations are permitted but often redundant unless defining a union type.

```typescript theme={"dark"}
// Explicit type annotation
function initializeExplicit(timeout: number = 3000): void {}

// Implicit type inference (timeout is inferred as 'number')
function initializeImplicit(timeout = 3000): void {}

// Explicit union type with a default
function initializeUnion(timeout: number | "infinite" = 3000): void {}
```

## Implicit Optionality

Assigning a default value implicitly marks the parameter as optional. You do not need to append the `?` modifier to the parameter name. In the generated declaration files (`.d.ts`), TypeScript represents default parameters as optional.

```typescript theme={"dark"}
function connect(protocol = "http") {}

// The inferred signature is:
// function connect(protocol?: string): void;

connect();       // Valid: protocol becomes "http"
connect("wss");  // Valid: protocol becomes "wss"
```

## The `undefined` vs. `null` Behavior

Default parameters are triggered strictly by omission or by passing the exact value `undefined`. Passing `null`, `false`, `0`, or `""` will bypass the default assignment, as these are considered valid, provided arguments.

```typescript theme={"dark"}
function setThreshold(limit = 50) {
  console.log(limit);
}

setThreshold();          // Evaluates to 50 (omitted)
setThreshold(undefined); // Evaluates to 50 (explicit undefined)
// setThreshold(null);   // Evaluates to null (Type error if strictNullChecks is enabled)
setThreshold(0);         // Evaluates to 0
```

## Parameter Ordering

Unlike standard optional parameters (using `?`), which must appear at the end of the parameter list, parameters with default values can be positioned anywhere. However, if a default parameter precedes a required parameter, the caller must explicitly pass `undefined` to trigger the default value.

```typescript theme={"dark"}
function configure(port = 8080, host: string) {}

// Invalid: Expected 2 arguments, but got 1.
// configure("localhost"); 

// Valid: Explicitly passing undefined triggers the default 'port'
configure(undefined, "localhost"); 
```

## Runtime Evaluation

Default parameters are evaluated at runtime, not compile time. The default value can be an expression, a function call, or a reference to preceding parameters in the same signature.

```typescript theme={"dark"}
// Evaluated per-call
function generate(id = Math.random()) {}

// Referencing preceding parameters (Temporal Dead Zone rules apply)
function calculateArea(width: number, height = width * 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>
