References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript const statement

Statement JavaScript All modern browsers Updated
Quick answer

The const keyword declares a block-scoped variable that can't be reassigned. const PI = 3.14; — trying PI = 3 throws. It's the recommended default for variables. Note that with objects and arrays, const only locks the variable, not the contents — you can still change what's inside.

Overview

const declares a variable you don't intend to reassign. Once set, the binding is fixed — const max = 100; max = 200; throws a TypeError. It's block-scoped like let, and it's the modern community default: reach for const first, and switch to let only when you discover the value really needs to change.

The point that trips up newcomers: const stops you reassigning the variable, but it does not make objects or arrays immutable. const user = {}; user.name = "Ada"; is perfectly fine — you're changing the object's contents, not rebinding user. Similarly you can push() into a const array. To actually freeze an object's contents, use Object.freeze().

Why default to it? Because "this never gets reassigned" is the common case, and saying so makes code easier to reason about — when you see const, you know that name always points to the same thing. It also catches accidental reassignment as an error. Reserve let for the genuinely changing values like counters and accumulators.

Syntax

const PI = 3.14159;
// PI = 3;  // TypeError: Assignment to constant variable

const user = { name: "Ada" };
user.name = "Grace";  // allowed - the object's contents can change

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const config = { theme: 'dark' };
  config.theme = 'light'; // contents can change

  let message;
  try {
    // reassigning the binding throws
    eval('const x = 1; x = 2;');
  } catch (e) {
    message = e.name;
  }

  document.getElementById('out').textContent =
    'theme: ' + config.theme + '\n' +
    'reassign error: ' + message; // theme: light / reassign error: TypeError
</script>

Best practices

  • Use const by default; switch to let only when you need to reassign.
  • Remember const objects and arrays are still mutable — their contents can change.
  • Use Object.freeze() when you need the contents to be read-only too.
  • Prefer it over var always for block scope and clarity.

Frequently asked questions

What is the difference between const and let?
A const variable can't be reassigned; a let one can. Both are block-scoped. Use const by default.
Can I change a const object?
Yes. const stops you reassigning the variable, but you can still modify the object's properties or push to a const array. Use Object.freeze() to lock the contents.
Why use const instead of let?
It signals that the value won't change, makes code easier to follow, and turns accidental reassignment into an error.
Does const make a value immutable?
No — only the binding. For primitives that's effectively immutable, but objects and arrays can still be modified internally.