const is not frozen

const fixes one thing: the line from the name to the value. The value keeps whatever mutability it had. An object behind a const still gains properties, an array still grows, and a Set still accepts elements. The keyword is about the binding, and the two ideas are easy to mix up because most examples in the wild use both at once.

JavaScript has no keyword that freezes a value at the point of declaration, and this lesson shows why the absence matters: a program that writes to a const object is legal, and a program that writes to the const name is a TypeError. The next lesson adds Object.freeze for the value itself.

The binding is fixed

A second assignment to a name declared with const throws TypeError: Assignment to constant variable. when the line runs, not when the file is parsed, so a function that never runs never throws. The program below catches the error and prints it, which keeps the rest of the output visible.

const answer = 42; try { answer = 43; } catch (error) { console.log(error.name + ': ' + error.message); } console.log('answer:', answer);

node constbind.mjs

TypeError: Assignment to constant variable.
answer: 42

The assignment on the fourth line is deliberately wrong: it is the line this section is about. The value behind the name never changed, and the error names the reason in one sentence. Nothing about the number 42 is immutable; the name simply refused to point somewhere else.

The contents still change

A property write is not an assignment to the binding. config.debug = true changes the object, and the binding keeps pointing at that same object. New properties may be added, and the runtime has no reason to complain: the object is an ordinary object that a const name happens to hold.

const config = { debug: false, retries: 3 }; config.debug = true; config.retries = 5; config.timeout = 1000; console.log(JSON.stringify(config));

node constobj.mjs

{"debug":true,"retries":5,"timeout":1000}

Three writes and no error: one changed an existing property, one changed another, and one added a property that the object did not have. That is the normal way to build a settings object, and it is why const is the right keyword for it: the name never has to move, so the keyword can promise that it never will.

Arrays under const

An array is an object whose properties have numbers for names. push writes the next index and the length, and numbers[0] = 0 writes an element. None of that assigns to the binding, so a const array is as changeable as a let array, and only the name is protected.

const numbers = [1, 2, 3]; numbers.push(4); numbers[0] = 0; console.log(numbers.join(', '));

node constarr.mjs

0, 2, 3, 4

The array grew and its first element changed, and the binding did not move. What const prevents is numbers = [], which would throw the same TypeError as the first program. A function that sorts a const array in place is legal for the same reason, and it is why the keyword says nothing about the argument a caller passes in.

A const object that grows

The combination is the idiom behind every counter object and every index: a const name for a container that is filled as the program runs. The program below counts words in a const object, adds a key the first time it meets a word, and increments it afterwards.

const counts = {}; for (const word of ['a', 'b', 'a']) { counts[word] = (counts[word] ?? 0) + 1; } console.log(JSON.stringify(counts));

node counts.mjs

{"a":2,"b":1}

The empty object never moved, and it ended the loop with two keys. ?? supplies the zero for the first sighting of a word, which the lesson on operators explains next to ||. A Map is the better container when the keys are not strings, and it grows under const in exactly the same way.

Exercises

  1. Remove the try block from constbind.mjs and run it. How many lines does the program print, and what is the exit status?
  2. Add config = {}; to the end of constobj.mjs inside a try block. Which message does the catch print?
  3. Sort the array in constarr.mjs with numbers.sort() before the print and explain why the call is allowed on a const.
  4. Change const counts = {}; to let counts = {}; in the last program. What changes in the output, and what does that tell you about the keyword?
  5. Declare a const binding for a number, then try to increment it with counter += 1 inside a function. Which error does the call throw?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does const fix about a binding?

    Answer

    That the name may not be assigned another value

    The keyword protects the line from the name to the value; the value keeps its own mutability.

  2. What does this program print? const config = { debug: false }; config.debug = true; console.log(config.debug);

    Answer

    true

    A property write changes the object, not the binding, so const has nothing to refuse.

  3. What does this program print? const list = [1]; list.push(2); console.log(list.length);

    Answer

    2

    push writes an element and the length property of the array object, which the const binding allows.

  4. What error does answer = 43; throw when answer is a const binding?

    Answer

    TypeError: Assignment to constant variable.

    The binding refuses the second assignment at run time, and the message names the variable type rather than the value.

  5. What does this program print? const counts = {}; for (const word of ["a", "a", "b"]) { counts[word] = (counts[word] ?? 0) + 1; } console.log(counts.a);

    Answer

    2

    The key a is met twice, so the nullish default supplies zero once and the counter ends at two.

  6. Which statement about a const array is true?

    Answer

    Its elements may change and it may grow

    Only the binding is protected, and every write through a method or an index reaches the array object.

  7. When is the TypeError of a const assignment thrown?

    Answer

    When the assignment line runs

    The check happens at run time, so a branch or a function that never runs never throws.

  8. What does const say about the object a function receives as an argument?

    Answer

    Nothing: the caller may still change the object

    const protects one binding in one scope, and a reference passed to a function is still a shared reference.

  9. What does this program print? const numbers = [1, 2, 3]; numbers[0] = 0; console.log(numbers.join(","));

    Answer

    0,2,3

    Writing an element is a property write on the array object, which a const binding permits.

  10. Which keyword combination gives a settings object whose name cannot move and whose properties can?

    Answer

    const without Object.freeze

    const fixes the name, and leaving the object unfrozen keeps its properties writable.

The code on this page was run with Node.js 24.22.1 and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image