Variables & Constants
Give your code a memory! Learn how to store names, numbers, and other information, update values, and write smarter JavaScript using let and const.
const name = "Alex";
let score = 10;
score += 5;
console.log(name, score);
What Is a Variable?
Imagine building a game that remembers a player’s name and score. You wouldn’t want to type those values into every line of code. Instead, you’d give each piece of information a name and let JavaScript remember it.
That’s what a variable does. It gives you a way to store a value and use it throughout your program.
let playerName = "Alex";
let score = 10;
console.log(playerName);
console.log(score);
10
Here, playerName stores text and score stores a number. You can refer to either value by its name.
Store Values That Can Change
Use let when a value may need to change as your program runs. Scores, counters, and changing messages are good examples.
let score = 10;
score = 20;
console.log(score);
The first line creates the variable. The second line assigns a new value. You don’t need to write let again when updating an existing variable.
You can also use the current value to calculate the next one.
let score = 10;
score = score + 5;
console.log(score); // 15
Keep a Value Consistent
Some values shouldn’t be reassigned after you create them. A website’s name or a fixed tax rate might be examples.
Use const when you don’t plan to assign a different value to the variable.
const websiteName = "mC0de";
console.log(websiteName);
If you try to reassign a constant, JavaScript reports an error.
const websiteName = "mC0de";
websiteName = "Another Site";
// TypeError: Assignment to constant variable.
const when a variable doesn’t need reassignment. Choose let when you know its value will change. Remember that const prevents reassignment, but it doesn’t make every property of an object unchangeable.Choose Names That Make Sense
Clear names make your code easier to understand. Compare let x = 25; with let studentAge = 25;. The second tells you what the number means.
const firstName = "Jordan";
let totalPoints = 100;
const favoriteLanguage = "JavaScript";
JavaScript Naming Rules
- Names can contain letters, numbers, underscores, and dollar signs.
- Names cannot begin with a number.
- Spaces and hyphens aren’t allowed in ordinary variable names.
- Names are case-sensitive:
scoreandScoreare different. - Reserved words such as
constcannot be variable names.
Most JavaScript developers use camelCase for variable names: start with a lowercase word, then capitalize the beginning of each following word.
Store Text, Numbers, and More
Variables can hold different kinds of information. For now, let’s explore text, numbers, and true-or-false values.
const studentName = "Taylor";
let lessonsCompleted = 2;
const isLearning = true;
console.log(studentName);
console.log(lessonsCompleted);
console.log(isLearning);
2
true
Quotation marks identify text. Numbers don’t need quotation marks, and true and false are special Boolean values.
We’ll explore these data types more closely in Lesson 03.
Use Variables to Build Messages
Once your values have names, you can combine them to create useful messages.
const name = "Alex";
let points = 15;
console.log(name + " has " + points + " points.");
The + symbol can join strings together. This is called concatenation.
Another useful option is a template literal, which uses backticks and places values inside ${...}.
const name = "Alex";
let points = 15;
console.log(`${name} has ${points} points.`);
Both examples produce the same message. Template literals are especially convenient when a sentence contains several variables.
Build Good Habits Early
Variables are simple, but choosing how to use them makes a big difference as your projects grow.
- Use
constby default when reassignment isn’t needed. - Use
letwhen the value must change. - Give variables descriptive names.
- Use camelCase consistently.
- Avoid creating multiple variables that mean the same thing.
- Keep related calculations easy to follow.
- Use
console.log()to inspect values while learning.
You’ll use variables in almost every JavaScript project, from simple buttons to full interactive applications.
Build Your Own Score Tracker
Create a small interactive scoreboard that remembers a player’s name and updates their score when they earn points.
Your Mission
- Create a
constvariable for the player’s name. - Create a
letvariable for the score, starting at zero. - Display the player’s name and score on the page.
- Make the button add 5 points whenever it’s clicked.
- Update the displayed score after every click.
- Change the player’s name and customize the button label.
Build Your Score Tracker!
Change the variables, customize your scoreboard, and run your code. Then click the button in the live result to earn points!
What You’ve Learned
You’re Giving JavaScript a Memory!
You’ve learned how to store information, update it, and use it to create messages and interactive experiences.
- How variables store information.
- When to choose
letorconst. - How to reassign and calculate new values.
- How to write clear variable names.
- How to combine variables into messages.
- How to use variables in an interactive scoreboard.
Next up: Data Types — discover the different kinds of information JavaScript can work with!
Your Learning Journey
0 / 17 CompletedKeep going! Every completed lesson brings you closer to mastering JavaScript.
Nice Work!
You've learned how to store information using variables, choose between let and const, update values, and build your own interactive score tracker. You're making excellent progress!


