# Understanding Variables and Data Types in JavaScript

When learning **JavaScript**, one of the first things you encounter is **variables** and **data types**.

Variables allow us to **store and manage information** inside a program.

For example:

*   A user’s **name**
    
*   A person’s **age**
    
*   Whether a student is **enrolled or not**
    

Understanding these concepts is essential because almost every program relies on variables.

* * *

# What Are Variables?

A **variable** is like a **box used to store information**.

Imagine you have labeled boxes:

| Box Label | Stored Value |
| --- | --- |
| name | Viral |
| age | 22 |
| city | Ahmedabad |

In programming, we store these values using variables.

Example:

```javascript
let name = "Viral";
let age = 22;
```

Here:

*   `name` stores `"Viral"`
    
*   `age` stores `22`
    

The program can **use or modify these values later**.

* * *

# Declaring Variables in JavaScript

In JavaScript, variables can be declared using:

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

Example:

```javascript
var city = "Ahmedabad";
let age = 22;
const country = "India";
```

Each keyword behaves slightly differently.

* * *

# var

`var` is the **older way** to declare variables.

Example:

```javascript
var language = "JavaScript";

console.log(language);
```

Output:

```plaintext
JavaScript
```

However, modern JavaScript developers prefer **let** and **const**.

* * *

# let

`let` is used when the value **may change later**.

Example:

```javascript
let score = 10;

score = 20;

console.log(score);
```

Output:

```plaintext
20
```

Here the value changed from **10 → 20**.

* * *

# const

`const` is used when the value **should not change**.

Example:

```javascript
const pi = 3.14;

console.log(pi);
```

If we try to change it:

```javascript
pi = 3.1415;
```

JavaScript will produce an **error**.

So `const` is used for **fixed values**.

* * *

# Primitive Data Types in JavaScript

Data types define **what type of value a variable holds**.

Here are the most common primitive data types.

* * *

# String

Strings represent **text values**.

Example:

```javascript
let name = "Viral Jain";
```

Examples of strings:

*   Names
    
*   Cities
    
*   Messages
    

* * *

# Number

Numbers represent **numeric values**.

Example:

```javascript
let age = 22;
let price = 199.99;
```

Numbers can represent:

*   age
    
*   marks
    
*   price
    
*   quantity
    

* * *

# Boolean

Booleans represent **true or false values**.

Example:

```javascript
let isStudent = true;
```

Boolean values are often used in **conditions**.

* * *

# null

`null` means **intentional empty value**.

Example:

```javascript
let selectedUser = null;
```

It means the variable **currently has no value assigned intentionally**.

* * *

# undefined

`undefined` means a variable **exists but has not been assigned a value yet**.

Example:

```javascript
let phoneNumber;

console.log(phoneNumber);
```

Output:

```plaintext
undefined
```

* * *

# Difference Between var, let, and const

| Feature | var | let | const |
| --- | --- | --- | --- |
| Modern JavaScript | No | Yes | Yes |
| Can change value | Yes | Yes | No |
| Recommended | No | Yes | Yes |
| Used for constants | No | No | Yes |

In modern JavaScript:

*   Use **let** for variables that change
    
*   Use **const** for fixed values
    

* * *

# What is Scope? (Beginner Explanation)

**Scope** means **where a variable can be accessed in your code**.

Example:

```javascript
{
  let message = "Hello World";
  console.log(message);
}
```

Inside the block `{ }`, the variable is accessible.

But outside:

```javascript
console.log(message);
```

This would cause an **error** because the variable exists only inside that block.

So scope defines **where variables live and can be used**.

* * *

# Assignment Practice

Try the following example in your browser console.

### Step 1: Declare Variables

```javascript
let name = "Viral";
let age = 22;
let isStudent = true;
```

### Step 2: Print Them

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

Output:

```plaintext
Viral
22
true
```

* * *

# Experiment With let and const

### Changing a let value

```javascript
let age = 22;
age = 23;

console.log(age);
```

Output:

```plaintext
23
```

* * *

### Changing a const value

```javascript
const country = "India";

country = "USA";
```

This will produce an **error** because `const` values cannot change.

* * *
