JavaScript Variables: let, const, and var
Master JavaScript variables. Understand let, const, var differences, scope, hoisting, and when to use each.
JavaScript Variables
What's a Variable?
A variable is like a labeled box where you store something. You give it a name, put a value inside, and use that name later to get the value back.
Think of it like this:
┌─────────────────────┐
│ Your Computer │
│ Memory │
│ │
│ ┌───────────────┐ │
│ │ name │ │ ← Label
│ │ "John" │ │ ← Value stored
│ └───────────────┘ │
│ │
│ ┌───────────────┐ │
│ │ age │ │ ← Label
│ │ 25 │ │ ← Value stored
│ └───────────────┘ │
└─────────────────────┘
When you write let name = 'John', you're telling JavaScript: "Create a box labeled 'name' and put 'John' inside it."
How to Create Variables
JavaScript gives you three ways to create variables, but you only need two of them:
1. const - For Things That Don't Change
const myName = 'John';
const birthYear = 1990;
Once you put a value in a const box, you can't change it. It stays the same.
When to use: Most of the time. If something shouldn't change, use const.
2. let - For Things That Change
let age = 25;
age = 26; // This works! You can change it
With let, you can change the value later.
When to use: When the value needs to change, like a counter or a score.
3. var - Don't Use This
var is the old way. It causes problems. Just use const or let instead.
Step-by-Step: Creating Your First Variable
Let's create a variable step by step:
Step 1: Decide what you want to store
// I want to store my name
Step 2: Choose const or let
// My name won't change, so use const
const
Step 3: Give it a name
const myName
Step 4: Put the value in
const myName = 'John';
Step 5: Use it
console.log(myName); // Shows: John
Where Variables Live (Scope)
Variables have a "home" - the place where they exist. This is called scope.
┌─────────────────────────────┐
│ function myFunction() { │
│ ┌─────────────────────┐ │
│ │ if (true) { │ │
│ │ let x = 10; │ │ ← x lives here
│ │ } │ │
│ │ console.log(x); │ │ ← ERROR! x doesn't exist here
│ └─────────────────────┘ │
│ } │
└─────────────────────────────┘
The variable x only exists inside the if block. Outside that block, it doesn't exist. This prevents bugs.
Real Example: Using Variables
Let's say you're building a simple calculator:
// Store the first number
const firstNumber = 10;
// Store the second number
const secondNumber = 5;
// Calculate the result
const result = firstNumber + secondNumber;
// Show the result
console.log(result); // Shows: 15
See how we stored values, used them, and got a result? That's what variables do.
Changing Values
With let, you can change the value:
let score = 0;
console.log(score); // 0
score = 10;
console.log(score); // 10
score = score + 5;
console.log(score); // 15
But with const, you can't:
const score = 0;
score = 10; // ERROR! Can't change a const
Quick Rules
- Use
constmost of the time - It's safer - Use
letwhen you need to change the value - Like counters or scores - Never use
var- It's outdated and causes problems - Give variables clear names -
userNameis better thanuorx
That's it! Variables are just boxes with labels. You put stuff in, you get stuff out. Start with const, use let when you need to change things.