# Understanding Variables and Data Types in JavaScript 

When we write programs, we need a place to **store information**.

For example:

*   Your **name**
    
*   Your **age**
    
*   Whether you are a **student**
    

In JavaScript, we store this information using **variables**.

Let’s understand this step by step in the **simplest way possible**.

* * *

# 1\. What Are Variables?

Think of a **variable like a box**.

This box can **store information**.

Example in real life:

```plaintext
Box → contains toys
Bottle → contains water
Wallet → contains money
```

In programming:

```plaintext
Variable → contains data
```

Example:

```plaintext
let name = "Rahul";
```

Here:

*   `name` → the **box**
    
*   `"Rahul"` → the **data inside the box**
    

We can now use this variable anywhere in the program.

```plaintext
console.log(name);
```

Output:

```plaintext
Rahul
```

* * *

# 2\. Why Do We Need Variables?

Variables help us **store and reuse data**.

Example:

```plaintext
let age = 25;

console.log(age);
console.log(age + 5);
```

Output:

```plaintext
25
30
```

Without variables, we would have to write numbers or values again and again.

Variables make programs **clean and easier to manage**.

* * *

# 3\. Declaring Variables in JavaScript

In JavaScript, we can create variables using:

*   `var`
    
*   `let`
    
*   `const`
    

Let’s look at each.

* * *

# 4\. Using `var`

`var` is the **older way** of declaring variables.

Example:

```plaintext
var city = "Delhi";

console.log(city);
```

Output:

```plaintext
Delhi
```

You can also **change the value later**.

```plaintext
var city = "Delhi";

city = "Mumbai";

console.log(city);
```

Output:

```plaintext
Mumbai
```

However, modern JavaScript usually prefers **let or const**.

* * *

# 5\. Using `let`

`let` is the **modern way to declare variables**.

Example:

```plaintext
let age = 20;

console.log(age);
```

You can **change the value later**.

```plaintext
let age = 20;

age = 21;

console.log(age);
```

Output:

```plaintext
21
```

Use `let` when the value **may change**.

* * *

# 6\. Using `const`

`const` means **constant**.

The value **cannot be changed**.

Example:

```plaintext
const country = "India";

console.log(country);
```

If you try to change it:

```plaintext
const country = "India";

country = "USA";
```

JavaScript will show an **error**.

So use `const` when the value **should stay the same**.

* * *

# Simple Comparison

| Keyword | Can Change Value? | Modern Usage |
| --- | --- | --- |
| var | Yes | Rarely used |
| let | Yes | Common |
| const | No | Very common |

* * *

# 7\. What Are Data Types?

Variables store **different kinds of data**.

These kinds are called **data types**.

Example:

```plaintext
Name → text
Age → number
IsStudent → true/false
```

JavaScript has many types, but beginners should know these **primitive data types**.

* * *

# 8\. Primitive Data Types in JavaScript

## 1\. String

A **string** is text.

Example:

```plaintext
let name = "Ravi";
```

Other examples:

```plaintext
"Hello"
"JavaScript"
"My Name"
```

Strings are written **inside quotes**.

* * *

## 2\. Number

Numbers represent **numeric values**.

Example:

```plaintext
let age = 25;
```

Examples:

```plaintext
10
100
3.14
```

Numbers can be used for **math calculations**.

* * *

## 3\. Boolean

Boolean means **true or false**.

Example:

```plaintext
let isStudent = true;
```

Possible values:

```plaintext
true
false
```

This is useful for **conditions and decisions**.

Example:

```plaintext
IsLoggedIn → true
IsLoggedIn → false
```

* * *

## 4\. Null

`null` means **empty value**.

Example:

```plaintext
let data = null;
```

It means the variable **exists but has no value**.

* * *

## 5\. Undefined

`undefined` means **a variable is declared but no value is assigned**.

Example:

```plaintext
let score;

console.log(score);
```

Output:

```plaintext
undefined
```

Because we **created the variable but did not give it a value**.

* * *

# 9\. What is Scope? (Simple Explanation)

Scope means:

> **Where a variable can be used in the code**

Example:

```plaintext
{
  let number = 10;
  console.log(number);
}
```

Inside the block → works.

But outside the block:

```plaintext
console.log(number);
```

It will show an **error**.

So scope means **the area where a variable is accessible**.

Simple idea:

```plaintext
Inside the box → variable works
Outside the box → variable may not work
```

* * *

# Assignment Practice

Try this in your browser console.

## 1\. Declare Variables

```plaintext
let name = "Arjun";
let age = 22;
let isStudent = true;

console.log(name);
console.log(age);
console.log(isStudent);
```

Output:

```plaintext
Arjun
22
true
```

* * *

## 2\. Change Value of `let`

```plaintext
let age = 22;

age = 23;

console.log(age);
```

Output:

```plaintext
23
```

Works because `let` **allows changes**.

* * *

## 3\. Try Changing `const`

```plaintext
const country = "India";

country = "USA";
```

Result:

```plaintext
Error
```

Because `const` **cannot be changed**.

* * *

# Simple Diagram

### Variable

```plaintext
name
 ↓
"Rahul"
```

### Data Types

```plaintext
String → "Hello"
Number → 25
Boolean → true
Null → empty value
Undefined → value not assigned
```
