Concept 2 of 10 Beginner ⏱️ 12 min

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?

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 (Score and score are different)

Best Practices (Should Follow)

  • Use descriptive names: playerScore instead of x
  • Use camelCase for multiple words: firstName, totalAmount
  • Start with lowercase letters
  • Make names meaningful but not too long
✓ Good Names
userName
totalPrice
isLoggedIn
itemCount
✗ Bad Names
x
data
thing
2fast

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);  // Error!
    let name = "Sam";
  • Forgetting to declare with let/const:
    score = 100;  // Works but bad practice!
    let score = 100;  // Better!
  • Trying to change a const:
    const pi = 3.14;
    pi = 3.14159;  // Error!

Quick Quiz

Key Takeaways

  • Variables are named containers that store data
  • Use let for values that change, const for 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