JavaScript - Constants (const)

Overview

Estimated time: 10–20 minutes

const creates a block-scoped binding that cannot be reassigned. Object contents can still change.

Learning Objectives

  • Declare constants with const.
  • Understand binding immutability vs object mutability.

Prerequisites

Examples

const rate = 0.08;
// rate = 0.1; // TypeError (cannot reassign binding)

const user = { id: 1, name: 'Ada' };
user.name = 'Ada Lovelace'; // allowed (object is mutable)
// Object.freeze(user) to prevent mutation

Common Pitfalls

  • Assuming const makes objects immutable. It only freezes the binding, not the contents.
  • Declaring without initializing is not allowed: const x; is a SyntaxError.