Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Const

const

TypeScript’s const creates a block-scoped binding that must be initialized and cannot be reassigned. The referenced object or array may still be mutable unless additional type-level or runtime measures are used.

How it works

Use const for a binding that should not be reassigned. It shares let's block scoping, requires initialization, and does not make an object or array deeply immutable; use readonly or a const assertion when type-level immutability or literal precision is needed.

Syntax

const variableName = initializer;

Key points

  • const is a variable-declaration form in TypeScript and JavaScript.
  • After initialization, the const binding cannot be assigned a different value.
  • const is block-scoped in the same way as let.
  • Prefer const for declarations whose bindings are not intended to change.

Common mistakes

  • const prevents rebinding, not mutation of the referenced object or array.

Example: A const binding protects the reference

const settings = { retries: 3 };
settings.retries = 4;

// settings = {}; // Error: cannot reassign a const binding
console.log(settings.retries);

The binding cannot be reassigned, but the referenced object can still be mutated.

When to use it

Stable bindings

A value such as a configuration reference, service instance, or imported dependency should not be rebound after setup.

Default local declarations

A declaration does not need to change during its scope, making least-privilege intent explicit.

Mutable referents

An object or array remains intentionally mutable while its reference stays stable.

Interview questions

QWhat does const guarantee in TypeScript?

const creates a block-scoped binding that must be initialized and cannot later be assigned a new value. It does not freeze the value held by that binding.

Follow-up: How would you preserve literal types in an object? Use an as const assertion, which also makes object properties readonly.

QWhen should a codebase prefer const over let?

Use const when a declaration will not be reassigned; use let when its binding must change. Both follow block-scoping rules, unlike var's function-scoped behavior.

Follow-up: Does const make an object deeply immutable? No; use readonly types or a const assertion for type-level restrictions.

QWhat happens if a const declaration is reassigned?

A const declaration must receive its initial value at the declaration site, and assigning to its binding afterward is an error. The binding can still refer to mutable state.

Follow-up: What does as const change? It prevents literal widening and gives object properties readonly types while producing readonly tuples for arrays.

  • let
  • var
  • readonly
  • as const

Source: Variable Declaration. Checked 2026-10-15.