Skip to main content
TypeScript provides two main ways to declare variables: let for values that can change, and const for values that remain constant.

Using let for Mutable Variables

The let keyword declares variables whose values can be reassigned:
let firstname: string = 'angel';
let lastname = 'diosdado';

let diceNumber = 5;
diceNumber = 3; // ✓ Valid - can reassign
TypeScript can infer types automatically. In the example above, lastname is inferred as string without an explicit type annotation.

Using const for Immutable Variables

The const keyword declares variables that cannot be reassigned:
const containsLetter = lastname.includes('a');
// containsLetter = false; // ✗ Error - cannot reassign const
Use const by default for variables that won’t be reassigned. This makes your code more predictable and easier to reason about.

Type Inference

TypeScript automatically infers types based on the assigned values:
let firstname: string = 'angel'; // Explicit type
let lastname = 'diosdado';        // Inferred as string
let diceNumber = 5;               // Inferred as number

Best Practices

  • Prefer const: Use const for values that won’t change
  • Use let sparingly: Only use let when you need to reassign the variable
  • Avoid var: The old var keyword has confusing scoping rules - stick with let and const
Never use var in modern TypeScript code. It has function-scoping instead of block-scoping, which can lead to bugs.

Build docs developers (and LLMs) love