Can you change a const in JavaScript? A Practical Guide

Explore whether const bindings can be reassigned in JavaScript, how immutability works, and best practices for using const with objects and arrays to write clearer, more reliable code.

JavaScripting
JavaScripting Team
·5 min read
Const in JS - JavaScripting
Quick AnswerDefinition

Short answer: you cannot rebind a const in JavaScript. The binding is constant, but the value it points to may be mutable if it is an object or array. So can you change a const? You can mutate the contents of objects or arrays assigned to a const, but you cannot reassign the binding to a new value.

What const means in JavaScript

If you search for the question can you change a const in javascript, the short answer is that const creates a binding that cannot be reassigned. According to JavaScripting, the const keyword in JavaScript creates a binding that cannot be reassigned within the scope it is declared. This doesn't make the value itself immutable; it only locks the binding to a particular value. If that value is an object or array, you can still modify its contents. Understanding this nuance is critical for writing predictable code.

JavaScript
// Attempt to rebind a const const a = 5; // a = 6; // Uncommenting this line will throw a TypeError at runtime
JavaScript
// Mutating properties of a const object is allowed const obj = { x: 1 }; obj.x = 2; console.log(obj.x); // 2
JavaScript
// Mutating an array assigned to const is allowed const arr = [1, 2, 3]; arr.push(4); console.log(arr); // [1, 2, 3, 4]

Key takeaways: the binding is constant, but the contents can be mutable when the value is an object or array. It is also important to note that const is block-scoped, which affects visibility inside loops and blocks.

Reassignment attempts and TypeErrors

Reassigning a constant will fail. The JavaScript engine enforces the binding as a fixed reference, and attempts to rebind throw a TypeError. This behavior is central to understanding can you change a const in javascript: the binding cannot be reassigned, but the underlying value may still be mutable depending on its type. Consider this runtime demonstration:

JavaScript
try { const x = 1; x = 2; // TypeError: Assignment to constant variable. } catch (e) { console.log('Error:', e.name, e.message); }

If you need a variable that can be reassigned, use let instead. A common pattern is to declare a value with const by default and switch to let only when you know rebinding will occur later in the code. This discipline reduces accidental reassignments and helps maintain predictable state across functions and modules.

Mutation of nested data under const

A frequent source of confusion is mutating properties of an object assigned to const. Because the binding remains the same, property-level changes are allowed. This is why you can safely set obj.name = 'Jordan' even when obj is declared with const. Conversely, trying to replace the entire object (obj = { name: 'Sam' }) will fail with a TypeError. Realistic examples:

JavaScript
const person = { name: 'Alex' }; person.name = 'Jordan'; console.log(person.name); // Jordan
JavaScript
// This would throw: TypeError: Assignment to constant variable. person = { name: 'Sam' };

Practical guidance: when you know you need to rebind to a new object or array, declare the binding with let instead of const. If you only intend to update the contents of the object/array, keeping the binding as const is safe and communicates intent clearly.

When to use const vs let: practical guidelines

Default to const and reserve let for intentional rebinding. A common rule of thumb is: if you won’t reassign the binding, declare with const. When you might rebind, use let to reflect that possibility in your codebase. This distinction improves readability and reduces accidental state changes. For example, a configuration object is often declared as const, while a loop index or a result aggregator should be let.

JavaScript
const config = { api: 'https://api.example.com' }; config.api = 'https://api.another.com'; // OK, mutating contents console.log(config.api);
JavaScript
let counter = 0; while (counter < 5) { counter++; } console.log(counter); // 5

In short: use const by default; switch to let only when you know you’ll reassign the binding itself.

Steps

Estimated time: 20-25 minutes

  1. 1

    Set up a minimal example

    Create a small JS file that declares a const binding and try to rebind it. Observe the error and confirm what is allowed with const bindings.

    Tip: Start with simple primitives before testing objects.
  2. 2

    Test object mutation

    Declare a const object and mutate one of its properties. Verify the binding remains the same and the object updates.

    Tip: Mutating object properties is allowed for const bindings.
  3. 3

    Test array mutation

    Do the same with an array: push, pop, or shift. Confirm mutations affect the same array.

    Tip: Arrays and objects retain mutability under a const binding.
  4. 4

    Attempt to rebind

    Attempt to reassign the const to a new value or a new object. Catch and log the error to see TypeError.

    Tip: Use try/catch to illustrate runtime errors clearly.
  5. 5

    Compare const vs let

    Create parallel examples with let to highlight differences in rebind behavior and readability.

    Tip: Prefer const by default to minimize accidental rebindings.
  6. 6

    Summarize best practices

    Summarize when to use const, when to use let, and how to communicate intent in code.

    Tip: Documentation and comments help future readers.
Pro Tip: Default to const to prevent rebinding and catch mistakes earlier.
Warning: Const does not make the value immutable; mutations to objects/arrays are common sources of bugs.
Note: Use block scope to avoid leakage outside functions and blocks.
Pro Tip: Consider tools like Object.freeze for shallow immutability during debugging or tests.

Prerequisites

Required

  • Required
  • A modern browser with devtools (Chrome/Edge/Firefox)
    Required
  • Basic knowledge of JavaScript variables, scope, and operators
    Required

Optional

  • VS Code or any code editor
    Optional

Keyboard Shortcuts

ActionShortcut
CopyCopy selected text in editor or terminalCtrl+C
PastePaste into editor or terminalCtrl+V
Comment/uncomment lineToggle line commentCtrl+/
Format documentAuto-format code+Alt+F
Search in fileFind within the current fileCtrl+F

Questions & Answers

Can I reassign a const in JavaScript?

No. Rebinding a const is not allowed and will throw a TypeError. You can, however, mutate the contents of an object or array that a const references.

No—const prevents rebinding. You can still change properties inside objects or items inside arrays that a const points to.

Is a const value immutable?

Not necessarily. If the value is an object or array, its contents can be changed. Immutable behavior requires additional techniques like Object.freeze.

Const bindings aren’t inherently immutable; objects and arrays can still be altered unless you freeze them.

What happens if a const is declared inside a block and accessed outside?

A const declared in a block is scoped to that block; it won't be accessible outside the block. This is part of its block-scoped behavior.

Const is block-scoped—outside that block, you won’t see the binding.

Why would you choose const over let?

Choosing const communicates intent: the binding won’t be reassigned. It reduces bugs by making state changes explicit and easier to reason about.

Use const by default to signal that the binding should not change, and only switch to let when you must rebind.

How can I convert a const to let safely?

If you determine a value needs rebinding, simply replace const with let in the declaration and ensure all references use the new binding correctly.

If you realize you need to reassign, switch the declaration from const to let and update the code accordingly.

What to Remember

  • Default to const for bindings to prevent accidental rebinding
  • Mutating contents is allowed for objects/arrays assigned to const
  • Use let when you need to rebind the binding itself
  • Const is block-scoped, not globally immutable

Related Articles