mc0de
JavaScript · Lesson 02 of 17

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.

JavaScript let & const Storing Data Hands-On Practice
● ● ●   variables.js
const name = "Alex";
let score = 10;

score += 5;

console.log(name, score);
CONSOLE OUTPUT
> Ready to run…
01 / Getting Started

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.

Your first variables
let playerName = "Alex";
let score = 10;

console.log(playerName);
console.log(score);
Console output
Alex
10

Here, playerName stores text and score stores a number. You can refer to either value by its name.

Think of it this way: A variable is like a labeled container. The label tells JavaScript which value you want to use.
02 / Using let

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.

Updating a score
let score = 10;

score = 20;

console.log(score);
Console output
20

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.

Adding points
let score = 10;

score = score + 5;

console.log(score); // 15
03 / Using const

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.

A constant value
const websiteName = "mC0de";

console.log(websiteName);

If you try to reassign a constant, JavaScript reports an error.

This causes an error
const websiteName = "mC0de";

websiteName = "Another Site";
// TypeError: Assignment to constant variable.
Good habit: Start with 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.
04 / Naming Variables

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.

Descriptive variable names
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: score and Score are different.
  • Reserved words such as const cannot be variable names.

Most JavaScript developers use camelCase for variable names: start with a lowercase word, then capitalize the beginning of each following word.

05 / Working With Values

Store Text, Numbers, and More

Variables can hold different kinds of information. For now, let’s explore text, numbers, and true-or-false values.

Different values
const studentName = "Taylor";
let lessonsCompleted = 2;
const isLearning = true;

console.log(studentName);
console.log(lessonsCompleted);
console.log(isLearning);
Console output
Taylor
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.

06 / Combining Information

Use Variables to Build Messages

Once your values have names, you can combine them to create useful messages.

Building a message
const name = "Alex";
let points = 15;

console.log(name + " has " + points + " points.");
Console output
Alex has 15 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 ${...}.

Template literals
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.

07 / Best Practices

Build Good Habits Early

Variables are simple, but choosing how to use them makes a big difference as your projects grow.

  • Use const by default when reassignment isn’t needed.
  • Use let when 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.

🚀 mC0de Challenge

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

  1. Create a const variable for the player’s name.
  2. Create a let variable for the score, starting at zero.
  3. Display the player’s name and score on the page.
  4. Make the button add 5 points whenever it’s clicked.
  5. Update the displayed score after every click.
  6. Change the player’s name and customize the button label.
Extra challenge: Add a second button that subtracts 5 points. Can you prevent the score from dropping below zero?
💻 Interactive JavaScript Sandbox

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!

Live Result
Lesson 02 Recap

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 let or const.
  • 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 Completed

Keep 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!