Variables
Learn how to store and manage data in your programs using variables—the building blocks of all code.
Variables are one of the most fundamental concepts in programming. They let your code remember and work with information—like a player's name, a game score, or whether someone is logged in.
What Is a Variable in Programming?
A variable is a name your program attaches to a value, so it can read that value later and replace it with a different one.
Think of a variable as a labeled box that holds information. The label is the variable's name, and what's inside the box is its value.
Just like you might have a box labeled "Photos" that contains your pictures, you might have
a variable called score that contains the number 100.
Real-World Analogy
Imagine a storage locker facility. Each locker has a unique number (the variable name), and you can put things inside (the value). You can also take things out and replace them with something else—variables work the same way!
Creating Variables in JavaScript
In JavaScript, you create a variable using the let keyword, followed by the
name you want to give it:
let score = 100; Let's break this down:
let— Tells JavaScript "I'm creating a variable"score— The name you chose for this variable=— The assignment operator (puts the value in the box)100— The value being stored;— Marks the end of the statement
Try It Yourself
Naming Variables
Variable names have rules and best practices:
Rules (Must Follow)
- Must start with a letter, underscore (_), or dollar sign ($)
- Can contain letters, numbers, underscores, and dollar signs
- Cannot contain spaces or hyphens
- Cannot be reserved keywords (like
let,if,function) - Are case-sensitive (
Scoreandscoreare different)
Best Practices (Should Follow)
- Use descriptive names:
playerScoreinstead ofx - Use camelCase for multiple words:
firstName,totalAmount - Start with lowercase letters
- Make names meaningful but not too long
userNametotalPriceisLoggedInitemCount xdatathing2fast let, const, and var
JavaScript has three ways to declare variables:
-
let— Use this for variables that will change. Most common choice. -
const— Use this for values that should never change (constants). -
var— The old way. Avoid using this in modern code.
let score = 0; // Can change later
const maxScore = 100; // Should never change
score = 50; // ✓ This works
maxScore = 200; // ✗ Error! Can't change a const Common Mistakes
- Using a variable before declaring it:
console.log(name); let name = "Sam"; // ReferenceError: Cannot access 'name' before initializationA
letorconstvariable exists from the top of its block, but you are not allowed to read it until the declaration line runs. That gap is called the temporal dead zone. - Forgetting to declare with let/const:
score = 100; console.log(score); // 100 — it works, but 'score' is now a globalAssigning to a name you never declared creates a global variable (and is an outright error in strict mode and inside JavaScript modules). Declare it instead:
let score = 100; console.log(score); // 100Note that you cannot write both versions in the same block — a second
let scorefor a name already in use is aSyntaxError: Identifier 'score' has already been declared. - Trying to change a const:
const pi = 3.14; pi = 3.14159; // TypeError: Assignment to constant variable.
Quick Quiz
Variables Quiz
What is a variable in programming?
Key Takeaways
- Variables are named containers that store data
- Use
letfor values that change,constfor values that don't - Choose descriptive, meaningful variable names
- Variable names are case-sensitive
- You can change a variable's value using the assignment operator (
=)
What's Next?
Now that you can store data in variables, let's learn about the different types of data you can store—numbers, text, true/false values, and more!
Finished this concept?
Mark it complete to track your progress