Learning objective: Declare variables with let and const, choose between them, reassign let variables safely and name variables so code explains itself.
Skills practised: Values and variables, Variables and constants.
04Explanation
A variable is a named place that stores a value so you can use it later. You create one with a declaration: let score = 10; creates a variable called score and puts 10 in it.
Use const for values that will not be replaced, such as a price or a product name. Use let for values that change, such as a running total or a counter. Start with const and switch to let only when you need to reassign.
To change a let variable, assign a new value without the keyword: score = score + 5;. Writing let score a second time in the same place is an error, and assigning to a const is an error too.
Good names say what the value means: itemCount is better than x. JavaScript names are case-sensitive (total and Total are different), cannot start with a digit and cannot contain spaces. The usual style is camelCase.
let and const create block-scoped bindings. They are hoisted to the top of their block but stay uninitialised until the declaration runs (the temporal dead zone), so reading them earlier throws a ReferenceError.
const makes the binding immutable, not the value: a const that holds an object or array can still have its contents changed. Immutability of data needs other techniques (copying, Object.freeze).
var is the legacy declaration: function-scoped, hoisted with the value undefined, and allowed to be redeclared. Modern code uses const by default and let where reassignment is required; linters commonly enforce prefer-const and no-var.
Names are identifiers: Unicode letters, $, _ and digits (not first). Reserved words such as class or return cannot be used. Clear names reduce the need for comments and make reviews faster.
05Definitions
Variable
A named storage place for a value.
Declaration
The statement that creates a variable, such as let count = 0;.
Assignment
Putting a new value into an existing variable with =.
let
Declares a variable that can be reassigned.
const
Declares a variable that cannot be reassigned.
camelCase
A naming style that joins words and capitalises each word after the first: cartTotal.
let score = 0;
// TODO: add 10 points for a correct answer
// TODO: add 10 points for another correct answer
// TODO: subtract 5 points for a hint
console.log("Score:", score);
Parking meter
Design variables for a small calculation from a description.
// A parking meter charges 2.5 per hour.
// A driver parks for 3 hours, then extends by 2 more hours.
09Prediction
Commit to an answer first, then run the real code to compare.
The Shopping Cart keeps product prices in constants and the quantities in variables that change as the shopper clicks. Stage 1 of the project uses exactly this split.
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Choose let or const: Prints the visitor count; storeName is declared with const; visitors is still a let
Track the score: Prints Score: 15; score holds 15; Uses += and -=
Parking meter: Prints hours and cost; ratePerHour is a const of 2.5; hours ends at 5; The cost is calculated
ex-006-01: output matches the documented expected output
ex-006-02: output matches the documented expected output
ex-006-03: output matches the documented expected output
ex-006-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-01-foundations/module-02-variables-values-and-types/lesson-006-variables-with-let-and-const/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
A variable stores a value under a name.
Use const by default and let when the value must change.
Reassign with = (or +=, -=) and without the keyword.
Choose names that explain the value; JavaScript is case-sensitive.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationlet versus const
Context: I am learning let and const in JavaScript.
Task: Explain when to use each with three examples from a shopping cart.
Constraints: Beginner level, no var, no objects.
Expected behaviour: I can choose the right keyword for any simple variable.
Testing: End with four declarations for me to classify, answers hidden at the bottom.
Code reviewReview my variable names
Context: I wrote a 10-line parking cost program.
Task: Suggest clearer names for my variables and explain why each is better.
Constraints: Do not change the logic. Keep camelCase.
Expected behaviour: Every name says what its value means.
Testing: List old name → new name so I can rename and rerun.
DebuggingAssignment to constant
Context: My program stops with TypeError: Assignment to constant variable.
Task: Help me find which line causes it by asking questions.
Constraints: Do not rewrite the program.
Expected behaviour: I can explain why the error happens and fix it myself.
Testing: Ask me to run it again after each change.
Exercise generationVariable drills
Context: I finished the parking meter exercise.
Task: Create two more small calculation exercises that need both let and const.
Constraints: Give exact expected output for each.
Expected behaviour: Each exercise has one value that changes.
Testing: Include the expected output to compare with mine.
Code predictionReassignment chain
Context: I can follow simple assignments.
Task: Give me five short programs that reassign let variables and ask what they print.
Constraints: Integers only, at most four lines each.
Expected behaviour: Each has one exact answer.
Testing: Reveal the answers only after I reply.