</>
Skip to content
JavaScript lessons (10/64)

JavaScript — Const

const = binding that can't be reassigned

const BIRTHDAY = "1990-05-01";
BIRTHDAY = "2000-01-01";   // TypeError: Assignment to constant variable

Declare and initialize in one step — there's no "later":

const PI;          // SyntaxError: missing initializer
PI = 3.14;

The modern default

Style guides agree: declare everything const, downgrade to let only when reassignment is truly needed. Why:

  1. Readers instantly know this name never flips
  2. Accidental reassignments become loud errors
  3. Intent becomes documentation

Objects: constant binding ≠ frozen value

const locks the name→value link, not the value's internals:

const user = { name: "Ada" };
user.name = "Grace";      // ✓ mutating properties is fine
user.age = 36;            // ✓ adding works too

user = {};                // ✗ TypeError — rebinding the NAME fails

Same story with arrays:

const colors = ["red"];
colors.push("blue");      // ✓ array grows
colors = [];              // ✗ error

Need true immutability? Ask explicitly:

Object.freeze(user);       // shallow-frozen object

Naming conventions

const MAX_USERS = 100;        // true constants: UPPER_SNAKE_CASE
const API_URL = "/api/v1";

const currentUser = getSession();  // regular values: normal camelCase

UPPERCASE signals "fixed configuration"; everyday consts stay camelCase.

Scope matches let

Block-scoped, no hoisting access before declaration line:

if (true) {
    const local = "here only";
}
// console.log(local) → ReferenceError

Common mistakes

Initializing late — must assign at declaration. Expecting const arrays/objects to be immutable — freeze explicitly if needed.

Mini Practice

  1. Trigger the assignment TypeError; read it twice
  2. Mutate a const object's property successfully — internalize the distinction
  3. Refactor five lets that never change into consts
  4. Try declaring without initializing; note the syntax error
  5. Freeze an object; attempt a mutation; observe silent failure (strict mode throws)

Next: arithmetic →

Related Topics

Frequently Asked Questions about Const

What is Const in JavaScript?

Const is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Const?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Const.

Why is Const important in JavaScript?

Const is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.