# Understanding Variables and Data Types in JavaScript

Hey Everyone,

In this blog, we will learn about JavaScript variables and datatypes.

When learning JavaScript, one of the first concepts you encounter is **variables**.

Variables help us **store information so that we can use it later in our program**.

### What is a Variable?

Think of a variable like a box.

You can store some information inside the box, and the label helps you find it later.

Example:

```javascript
Box Label → name
Value inside → "Rahul"
```

In JavaScript:

```javascript
let name = "Rahul";
```

Here:

*   `name` → variable name (the label)
    
*   `"Rahul"` → value stored inside the variable
    

* * *

### Why Do We Need Variables?

Variables allow us to **store and reuse values**.

Example:

```javascript
let price = 100;
let quantity = 3;

let total = price * quantity;

console.log(total); // 300
```

Instead of writing numbers everywhere, variables make the code **clear and reusable**.

* * *

### Declaring Variables in JavaScript

JavaScript provides **three ways** to declare variables:

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

* * *

### 1\. var

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

Example:

```javascript
var city = "Delhi";

console.log(city);
```

You can change the value later.

```javascript
var city = "Delhi";

city = "Mumbai";

console.log(city); // Mumbai
```

* * *

### 2\. let

`let` is the **modern way** of declaring variables.

Example:

```javascript
let age = 20;

console.log(age); //20
```

You can also change the value later.

```javascript
let age = 20;

age = 21;

console.log(age); //21
```

* * *

### 3\. const

`const` is used for values that **should not change**.

Example:

```javascript
const country = "India";

console.log(country);
```

Trying to change it will cause an error.

```javascript
const country = "India";

country = "USA";
```

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

* * *

### Primitive Data Types in JavaScript

JavaScript has several **primitive data types**.

Let’s look at the most common ones.

* * *

### 1\. String

A **string** represents text.

Example:

```javascript
let name = "Abhi";
```

* * *

### 2\. Number

Numbers represent numeric values.

Example:

```javascript
let age = 24;
let price = 919.99;
```

* * *

### 3\. Boolean

Booleans represent **true or false values**.

Example:

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

* * *

### 4\. null

`null` means a variable intentionally has **no value**.

Example:

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

It means the variable exists but currently **does not hold any value**.

* * *

### 5\. undefined

`undefined` means a variable was created, but **no value has been assigned yet**.

Example:

```javascript
let score;

console.log(score); // undefined
```

* * *

### Basic Difference Between var, let, and const

| Feature | var | let | const |
| --- | --- | --- | --- |
| Can change value | Yes | Yes | No |
| Modern usage | Rare | Recommended | Recommended |
| Can redeclare variable | Yes | No | No |

In modern JavaScript:

*   Use `let` for values that may change
    
*   Use `const` for values that should remain constant
    

* * *

### What is Scope?

Scope determines **where a variable can be accessed in the code**.

Think of scope like **rooms in a house**.

If a variable is created inside a room, it can only be used inside that room.

Example:

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

Inside the block → it works.

But outside:

```javascript
{
  let message = "Hello";
}

console.log(message);
```

This will cause an **error**, because the variable only exists inside the block.

* * *

### Simple Scope Visualization

```plaintext
Global Scope
   |
   |---- variable A
   |
   |---- Block Scope
           |
           |---- variable B
```

`variable B` can only be accessed inside the block.

`let` and `const` are block scope

### VAR **is a global scope?**

`var` is **function-scoped**, not truly global.

If you declare it inside a function, it stays inside that function.

```javascript
function test() {
  var x = 10;
}

console.log(x); // Error
```

`x` cannot be accessed outside the function.

However, if `var` is declared **outside any function**, then it becomes global.

```javascript
var x = 10;

console.log(x); // works
```

So `var` is often *seen* as global because many beginners declare it outside functions.

* * *

### Practice Assignment

Try this in your browser console.

* * *

### Step 1: Declare Variables

```javascript
let name = "Rahul";
let age = 20;
let isStudent = true;
```

* * *

### Step 2: Print the Values

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

* * *

### Step 3: Change a let Value

```typescript
age = 21;
console.log(age);
```

Observe how the value changes.

* * *

### Step 4: Try Changing const

```javascript
const country = "India";

country = "USA";
```

Observe the error JavaScript gives.

* * *

And now, you know what variables and data types in JS are.

If you have any doubt or want to connect, feel free to drop a comment — I’d be happy to help.

Thanks for reading, and see you in the next blog!

Peace ✌️ and Happy Learning!
