Variables
Store, update, and manage data in JavaScript using const and let keywords.
Learning Goals
The Core Concept
Variables are containers that store data values in your code. Think of them like labeled storage boxes. In JavaScript, we have three ways to declare variables: `const` (for values that never change), `let` (for variables whose values can be changed later), and `var` (the old keyword which is no longer recommended because it behaves unpredictably). Always default to using `const` unless you know you will need to reassign the value later.
Visual guide
JavaScript concept flow
A simple original diagram to connect the lesson idea with real project flow.
Code & Implementation
// A value that does not change
const taxRate = 0.18;
// A value that can change as the program runs
let userScore = 10;
userScore = userScore + 5; // Updated to 15
console.log(taxRate);
console.log(userScore);Expected Output
0.18 15
Build a Shopping Cart Calculator
Hands-on practice task
The Challenge
Declare a constant price set to 199, a variable quantity set to 2, and update quantity to 3. Calculate the total cost and store it in a constant named total.
Helpful Hints
- •Use const for price and total.
- •Use let for quantity since it changes.
Quick Knowledge Check
What happens if I try to change a const variable?
Do I have to declare a variable before using it?
Continue Learning
Next steps after this lesson
Declare a constant price set to 199, a variable quantity set to 2, and update quantity to 3. Calculate the total cost and store it in a constant named total.
Ready to put your coding skills to the test?
Don't just read—write code! Use our free Try-Code Playground to experiment with real-time preview, or search utilities on our Developer Tools List.