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
```javascript 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
```javascript 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 ```javascript // I want to store my name ```
**Step 2:** Choose const or let ```javascript // My name won't change, so use const const ```
**Step 3:** Give it a name ```javascript const myName ```
**Step 4:** Put the value in ```javascript const myName = 'John'; ```
**Step 5:** Use it ```javascript 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:
```javascript // 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:
```javascript 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:
```javascript const score = 0; score = 10; // ERROR! Can't change a const ```
Quick Rules
1. **Use `const` most of the time** - It's safer 2. **Use `let` when you need to change the value** - Like counters or scores 3. **Never use `var`** - It's outdated and causes problems 4. **Give variables clear names** - `userName` is better than `u` or `x`
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.