JavaScript lesson · 10 min
JavaScript Variables and Constants
Use let and const to store values, build strings, and print predictable output.
What you will practice
- Create variables with let and const.
- Update values intentionally.
- Use template strings for readable output.
What this means
A variable is a named value. Instead of writing the same value again and again, you give it a name and let the program refer to that name.
In JavaScript, const means the name should keep pointing to the same value. let means the value may change later. This small difference makes code easier to read because it tells the next person what you expect to happen.
Variables are not only for numbers. They can hold text, true/false values, arrays, objects, dates, and results returned by functions.
If this is your first time seeing this
When you see `const name = "Mira"`, read it as: create a label called `name`, and put the text `Mira` under that label.
The equal sign in JavaScript means assignment, not a math equation. It puts the value on the right into the name on the left.
Mini glossary
- Variable
- A named place for a value, such as text, a number, or true/false.
- const
- Creates a name that should not be reassigned to another value.
- let
- Creates a name that is allowed to receive a new value later.
Example from everyday life
Think of variables like labeled boxes on a desk. One box says `userName`, another says `cartTotal`, another says `isLoggedIn`. The label lets you find the right thing quickly without remembering every detail in your head.
How it works step by step
- JavaScript reads `const name = "Mira"` and creates a name called `name` with the text value `Mira`.
- It reads `let completedLessons = 1` and creates a value that is allowed to change.
- The line `completedLessons = completedLessons + 1` takes the old value, adds one, and stores the new value back under the same name.
- The `console.log` lines print text so you can see what the program calculated.
Where you will use this
- An online store stores `price`, `quantity`, and `cartTotal` while the user changes the cart.
- A login form stores `email`, `password`, and `isSubmitting` while it sends a request.
- A settings page stores the selected theme, language, or notification preference.
Before you run the code
Variables are named places for values. In modern JavaScript, use const when a binding should not be reassigned and let when the value needs to change.
A template string uses backticks and ${...} placeholders. It is a clean way to combine text with values without a chain of plus signs.
Common beginner mistakes
- Trying to change a `const` value later. If the value must change, use `let`.
- Forgetting quotation marks around text. `Mira` without quotes means a variable name, not text.
- Using names that do not explain the value, such as `x` or `data`, when `completedLessons` would be clearer.
Run the code to see console output here.
Code runs locally in a temporary browser worker with a timeout. It is not sent to Lumio analytics or executed on the server.
Try changing this next
- Change the name from `Mira` to your own name and run the code again.
- Change `completedLessons` from `1` to `5` and predict the output before running it.
- Add one more `console.log` line that prints a short sentence using both variables.