JavaScript Variables
Table of Contents + −
In the previous lesson, we learned the different ways to run JavaScript. Now let’s learn how to store data using variables.
📦 What is a Variable?
A variable is a named container that stores a value. You can think of it like a labeled box: you put a value inside, give the box a name, and use that name whenever you need the value again.
For example, instead of writing your score everywhere in your code, you store it once in a variable and refer to it by name.
✍️ Declaring a Variable
In modern JavaScript, you create a variable using const or let, followed by a name, an equals sign, and a value.
const userName = "Alex";let score = 0;Let’s break this down:
| Part | What it does |
const or let | The keyword that creates the variable |
userName | The name you give the variable |
= | Assigns the value on the right to the variable |
"Alex" | The value stored in the variable |
Once a variable is created, you can use its name anywhere in your code:
const userName = "Alex";console.log(userName); // prints: AlexHere we declare and then read the variable back:
- The first line creates
userNameand stores the value"Alex"in it. - The second line passes
userNametoconsole.log, which looks up the stored value and printsAlex.
🔁 Changing a Variable’s Value
A variable created with let can be given a new value later. This is called reassigning.
let score = 0;console.log(score); // prints: 0
score = 10;console.log(score); // prints: 10Let’s walk through what happens line by line:
- We declare
scorewithletand set it to0. - The first
console.logreadsscoreand prints0. - We reassign
scoreto10. Because there is nolethere, we are updating the existing variable, not creating a new one. - The second
console.logreadsscoreagain and prints the new value,10.
A variable created with const cannot be reassigned. Trying to change it causes an error.
const userName = "Alex";userName = "Sam"; // ❌ Error: cannot reassign a constWe create userName with const on the first line, then try to give it a new value on the second line. Because const values are locked once set, JavaScript stops with an error instead of changing it.
const vs let
Use const when the value will not be reassigned, and let when it will. We
will look at this in detail in the next lesson.
🏷️ Rules for Naming Variables
Variable names follow a few rules:
- Start with a letter,
_, or$- names cannot start with a number - No spaces - use
camelCaseto join words, likefirstName - Case-sensitive -
scoreandScoreare different variables - No reserved words - you cannot use keywords like
constorletas names
// ✅ Correctconst firstName = "Alex";const totalScore = 100;const _count = 5;
// ❌ Wrongconst 1stName = "Alex"; // starts with a numberconst first name = "Alex"; // contains a spaceHere is what separates the valid names from the broken ones:
firstName,totalScore, and_countall start with a letter or_and contain no spaces, so they are valid.1stNameis invalid because it starts with a number.first nameis invalid because the space splits it into two pieces JavaScript cannot read as one name.
Use clear names
Choose names that describe the value. userAge is much clearer than x. Good
names make your code easy to read and understand.
⚠️ Common Mistakes to Avoid
| Mistake | Problem | Solution |
Reassigning a const | Causes an error | Use let if the value needs to change |
| Using a variable before declaring it | Causes an error | Declare the variable before you use it |
Unclear names like a or x | Hard to read and understand | Use descriptive names like userName |
🔧 Try It Yourself!
- Create a
constcalledcityand store your city name in it. - Create a
letcalledcountand set it to0, then reassign it to5. - Print both variables to the console using
console.log. - Try reassigning your
constand notice the error in the console.
🧩 What You’ve Learned
- ✅ A variable is a named container that stores a value
- ✅ You create variables using
constorlet - ✅
letvariables can be reassigned,constvariables cannot - ✅ Variable names have rules and should be clear and descriptive
- ✅ JavaScript is case-sensitive, so naming matters
🚀 What’s Next?
You now know how to store data in variables. Next, we will look more closely at the difference between let and const and when to use each. Let’s continue to let vs const.