> ## 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 const Variable

A `const` declaration in TypeScript defines a block-scoped variable with a constant reference to a value. Once initialized, the identifier cannot be reassigned to a new memory location. However, `const` enforces *binding immutability*, not *value immutability*; the internal properties of objects and arrays assigned to a `const` variable remain mutable at runtime.

## Syntax and Initialization

A `const` variable must be initialized at the exact moment of its declaration. It cannot be declared and assigned later.

```typescript theme={"dark"}
const MAX_RETRIES: number = 3; // Valid

const TIMEOUT: number; 
// TS Error: 'const' declarations must be initialized.
```

## TypeScript Type Inference (Literal Types)

While JavaScript treats `const` purely as a runtime constraint, the TypeScript compiler uses `const` to alter type inference. When a primitive value is assigned to a `const` variable, TypeScript infers a **literal type** rather than widening it to a general primitive type.

```typescript theme={"dark"}
let mutableStatus = "active"; 
// Inferred type: string (Type Widening)

const immutableStatus = "active"; 
// Inferred type: "active" (Literal Type)
```

## Scope and Temporal Dead Zone (TDZ)

Like `let`, `const` is block-scoped, meaning it only exists within the nearest enclosing `{}` block. It is also subject to the Temporal Dead Zone (TDZ), meaning the variable cannot be accessed before its line of declaration is executed.

```typescript theme={"dark"}
{
  // console.log(API_URL); // ReferenceError: Cannot access 'API_URL' before initialization
  const API_URL = "https://api.example.com";
}
// console.log(API_URL); // ReferenceError: API_URL is not defined (out of scope)
```

## Reference vs. Value Immutability

Reassigning the identifier throws a compiler error, but mutating the properties of a complex data structure (like an object or array) is perfectly valid.

```typescript theme={"dark"}
const config = { port: 8080, env: "dev" };

config.port = 9000; // Valid: Mutating the underlying object
config.env = "prod"; // Valid

config = { port: 3000, env: "test" }; 
// TS Error: Cannot assign to 'config' because it is a constant.
```

## Const Assertions (`as const`)

To enforce deep immutability at the type level and prevent type widening for objects and arrays, TypeScript provides the `as const` assertion. This casts all properties to `readonly` and infers the narrowest possible literal types.

```typescript theme={"dark"}
const strictConfig = { 
  port: 8080, 
  env: "dev" 
} as const;

// Inferred type:
// {
//   readonly port: 8080;
//   readonly env: "dev";
// }

strictConfig.port = 9000; 
// TS Error: Cannot assign to 'port' because it is a read-only property.
```

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