# Understanding Variables and Data Types in JavaScript

When you start programming, one of the first things you learn is **how to store information**.

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

Before jumping into syntax, let’s understand this with a simple real-life analogy.

## A Simple Analogy: Variables as Boxes

Imagine you have several boxes at home.

Each box can store something different:

*   One box stores your **name**
    
*   One box stores your **age**
    
*   One box stores whether you are a **student**
    

To identify them easily, you put **labels** on each box.

Programming works in a very similar way.

A **variable** is like a **labeled box** that stores information.

```plaintext
Name Box   → "Sayantan"
Age Box    → 21
Student Box → true
```

In JavaScript, we give these boxes **names** so we can access the values later.

## What Are Variables?

A **variable** is a container used to store data that can be used later in a program.

For example:

```javascript
let name = "Sayantan";
let age = 21;
let isStudent = true;
```

Here:

*   `name` stores text
    
*   `age` stores a number
    
*   `isStudent` stores a boolean value (true/false)
    

We can print them using:

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

Output:

```shell
Sayantan
21
true
```

## Declaring Variables in JavaScript

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

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

## 1️⃣ Using `var`

`var` was the **original way** to declare variables in JavaScript.

```javascript
var city = "Kolkata";
console.log(city);
```

You can also change the value later.

```javascript
var city = "Kolkata";
city = "Delhi";

console.log(city);
```

Output:

```shell
Delhi
```

However, `var` is **less commonly used today** because it has some confusing behaviors.

## 2️⃣ Using `let`

`let` is the **modern way** to declare variables that may change later.

```javascript
let score = 50;
score = 80;
console.log(score);
```

Output:

```shell
80
```

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

## 3️⃣ Using `const`

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

```javascript
const country = "India";
console.log(country);
```

If we try to change it:

```javascript
const country = "India";
country = "USA";
```

JavaScript will throw an error.

```shell
TypeError: Assignment to constant variable
```

Use `const` when the value should stay **fixed**.

## Primitive Data Types in JavaScript

A **data type** tells JavaScript what kind of value is stored inside a variable.

JavaScript has several primitive data types.

For beginners, these are the most important ones:

*   String
    
*   Number
    
*   Boolean
    
*   Null
    
*   Undefined
    

NO! NOT OBJECT❌ I made mistake multiple times

## String

A **string** represents text.

Example:

```javascript
let name = "Sayantan";
```

Examples of strings:

```shell
"Hello"
"JavaScript"
"My name is Sayantan"
```

## Number

A **number** represents numeric values.

Example:

```javascript
let age = 21;
let price = 99.99;
```

Numbers can be:

*   Integers → `10`
    
*   Decimals → `10.5`
    

## Boolean

A **boolean** has only two possible values:

```shell
true
false
```

Example:

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

Booleans are often used in **conditions and decision making**.

## Null

`null` means **intentional absence of a value**.

Example:

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

This means the variable exists but **currently has no value**.

## Undefined

`undefined` means a variable has been declared but **no value has been assigned yet**.

Example:

```javascript
let marks;
console.log(marks);
```

Output:

```shell
undefined
```

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

![](https://cdn.hashnode.com/uploads/covers/69517129b3f42beb72514f64/36753554-48a4-417e-92e4-c88a547c0ff1.png align="center")

In modern JavaScript:

*   Use `const` **by default**
    
*   Use `let` **when values need to change**
    
*   Avoid `var` **in most cases**
    

## What is Scope? (Beginner Explanation)

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

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

If you keep an object inside one room, it may not be visible from another room.

Example:

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

console.log(message);
```

Output:

```shell
Hello
ReferenceError
```

Why?

Because `message` was created **inside the block** `{ }`.

So it cannot be accessed outside it.

This is called **block scope**.

## Practical Example

Let’s store some real information in variables.

```javascript
let name = "Sayantan";
let age = 21;
let isStudent = true;

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

Output:

```shell
Name: Sayantan
Age: 21
Is Student: true
```

# Small Assignment (Practice)

Try writing this code yourself.

### Step 1: Declare variables

```plaintext
let name = "Your Name";
let age = 20;
let isStudent = true;
```

### Step 2: Print them

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

### Step 3: Try changing values

```plaintext
age = 25;
console.log(age);
```

### Step 4: Try modifying a const variable

```plaintext
const country = "India";
country = "USA";
```

Observe the error JavaScript gives.

# Scope Visualisation Diagram

![](https://cdn.hashnode.com/uploads/covers/69517129b3f42beb72514f64/7abc35e6-899e-4da1-8173-fd3f4ff39021.png align="center")

Variables inside the **block scope** cannot be accessed outside.
