# Understanding Variables and Data Types in JavaScript

When you're writing code, you need to store information somewhere. Names, numbers, results from calculations, user input—all of this gets saved in something called variables. Let's break down what they are and how they work.

## Variables: The Boxes of JavaScript

Think of a variable as a labeled box. You write something on the box (the variable name) and put something inside (the value).

```javascript
let name = "Sarah";
```

Here, `name` is the label on the box, and `"Sarah"` is what's inside it.

You need variables because code doesn't just run once—it processes data. That data changes constantly. A user's name changes, a cart total changes, a score changes. Variables let your code keep track of these changing values.

## Declaring Variables: var, let, and const

JavaScript gives you three ways to declare variables:

```javascript
var age = 25;
let score = 100;
const PI = 3.14;
```

Here's the difference:

*   `let` – Can be changed later
    
*   `const` – Cannot be changed (constant)
    
*   `var` – The old way, similar to let but with different scope behavior
    

```javascript
let count = 1;
count = 2; // Works fine

const taxRate = 0.15;
taxRate = 0.20; // Error! Can't change const
```

For most situations, use `let` when you need to update a value and `const` when the value should stay fixed.

## Primitive Data Types

JavaScript has several basic data types:

**String** – Text data, wrapped in quotes:

```javascript
let name = "John";
let message = 'Hello, world!';
```

**Number** – Both integers and decimals:

```javascript
let age = 25;
let price = 19.99;
```

**Boolean** – True or false:

```javascript
let isLoggedIn = true;
let hasPremium = false;
```

**Null** – Intentional absence of value:

```javascript
let user = null;
```

**Undefined** – Variable declared but not assigned:

```javascript
let email;
console.log(email); // undefined
```

## A Bit About Scope

Scope is simply "where in your code the variable can be used." A variable declared inside a function is only available inside that function. We'll get into this more later, but for now know that `let` and `const` are block-scoped—they're contained within the curly braces where they're defined.

```javascript
if (true) {
  let inside = "only here";
}
console.log(inside); // Error - not accessible outside
```

## Assignment: Try It Yourself

Here's some code to play with:

```javascript
// Declare variables
let name = "Alice";
let age = 22;
const isStudent = true;

// Print them
console.log("Name:", name);
console.log("Age:", age);
console.log("Is Student:", isStudent);

// Try changing values
name = "Alice Smith";    // Works - let can be reassigned
age = 23;                // Works - let can be reassigned
// isStudent = false;    // Error! Can't change const

console.log("Updated name:", name);
console.log("Updated age:", age);
```

Run this in your browser console or Node, and you'll see how `let` allows changes while `const` protects values.

## Quick Reference

| Keyword | Can Change? | Scope | When to Use |
| --- | --- | --- | --- |
| `let` | Yes | Block | Values that change |
| `const` | No | Block | Values that stay the same |
| `var` | Yes | Function | Legacy code only |

## Wrapping Up

Variables store data that your code uses and modifies. Pick `const` by default, switch to `let` when you need to update values, and use clear names like `userName` instead of vague ones like `x`. These small habits make your code readable and easier to work with.
