Store and Update Values with const and let
Quick answer
A binding associates a name with a value. Use const when you do not intend to assign another value to that name; use let when the binding must change. A declaration introduces a name, while reassignment updates an existing mutable binding.
How it works
The syntax const price = 7; declares and initializes a constant binding. let count = 2; declares a mutable binding. Later count = 4; assigns a new value. Names are case-sensitive. In these examples all declarations and reads occur in the same scope.
const price = 7;const quantity = 4;console.log(price * quantity);Expected console output (Node.js):
28Each name supplies the value assigned at declaration.
let total = 10;total = total + 3;console.log(total);Expected console output (Node.js):
13The right-hand expression reads the old total before assignment updates it.
Common mistakes
Do not reassign a const binding or read a let/const binding before it is initialized. A const binding does not make an object immutable; objects and scope are covered in later chapters.
Practice
Start with a count of two, add three, then display the resulting count. Decide which names should use const and which should use let.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Store and Update Values with const and let — Knowledge Check
Review the concept and apply it to a new example.
Which declaration is suitable for a total you will reassign?
After let count = 2; count = count + 3; what is count?
Which declaration expresses no intended reassignment?
Does const guarantee that all contents of an object are immutable?
If a name is declared as count, is Count automatically the same name?