JavaScript const: Bindings Versus Object Mutation
Quick answer
const prevents reassignment of a binding. It does not freeze the value stored through that binding. Use it when a name should continue referring to the same value throughout its scope.
How it works
In an ordinary declaration, write const name = value; with an initializer. A later assignment to the name throws TypeError. Preview: { count: 1 } creates an object with a property named count; settings.count = 2 changes that property, not the binding. Objects are covered fully later. Like let, const is lexical and block-scoped.
Expected output in Node.js:
15Neither name needs reassignment.
const settings = { count: 1 };settings.count = 2;console.log(settings.count);Expected output in Node.js:
2The object remains the same object.
Common mistakes
const rate; is invalid in a normal declaration, and assigning rate = 9 later is not allowed. Do not infer deep immutability from const. A special loop declaration such as for-of follows different initialization rules, taught later.
Practice
Use a fixed conversion factor and a changing quantity. Calculate the total without reassigning the factor.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript const: Bindings Versus Object Mutation — Knowledge Check
Apply the taught concept and reason about the result.
Which ordinary declaration is valid?
What happens after const fee = 2; fee = 3;?
What does const basket = { count: 2 }; basket.count = 5; console.log(basket.count); print?
Which claim about const is accurate?
Which should be declared const in a simple calculation?