| sidebar_position | 2 |
|---|---|
| title | Variables: Giving JS a Memory |
| sidebar_label | Variables |
| description | Learn how to store and manage data using let and const. |
Imagine you are moving into a new house. You have a lot of stuff, so you put your things into boxes and write labels on them (like "Kitchen Supplies" or "Books").
In JavaScript, a Variable is just a box with a label that holds a piece of information.
To create a variable, we use a keyword, give it a name, and assign a value to it using the = sign.
let playerName = "Ajay";
let playerScore = 100;- The Keyword: (
letorconst) This tells the computer you are creating a new "box." - The Label (Name): This is how you will refer to the data later.
- The Value: The actual data you are storing inside the box.
In modern JavaScript (2026), we use two main keywords. Choosing the right one is very important!
Use let when the information inside the box will change later (like a score in a game or a timer).
let score = 0;
score = 10; // This is allowed!Short for "Constant." Use const for information that should never change (like your birthday or a fixed setting).
Pro Tip: If you aren't sure, use const first!
const birthYear = 2000;
birthYear = 2005; // ❌ ERROR! You can't change a constant.Let's see how variables work in real-time. In this CodePen, we have a "Counter" app. One variable keeps track of the number!
- In the JS tab, look for
let count = 0;. Change the0to100. Notice the starting number changes! - Try changing the keyword
lettoconst. Click the "Increase" button. Does it still work? (Hint: Check your console for an error!)
You can't just name your variables anything. JavaScript has a few rules:
- Do use camelCase: Start with a small letter, and every new word starts with a Capital (e.g.,
userProfilePicture). - Do use descriptive names:
let price = 10;is better thanlet p = 10;. - Don't start with a number:
let 1stPlace = "Gold";will break your code. - Don't use spaces:
let user name = "Ajay";is not allowed.
- [x] I understand that a variable is a container for data.
- [x] I know that
letis for values that change. - [x] I know that
constis for values that stay the same. - [x] I practiced naming variables using camelCase.