# Understanding Variables and Data Types in JavaScript

### JavaScript Variables and Data Types (Beginner Friendly Guide)

Every program needs a way to **store information**.  
In JavaScript, this is done using **variables**.

In this guide we will learn:

*   What variables are
    
*   How to declare variables using var, let, and const
    
*   Primitive data types (string, number, boolean, null, undefined)
    
*   The difference between var, let, and const
    
*   What **scope** means (simple explanation)
    

* * *

# 1\. What Are Variables?

Think of a **variable like a box that stores information**.

Example:

```plaintext
Box label → Name
Inside box → "Tushar"
```

In JavaScript:

```plaintext
let name = "Tushar";
```

Here:

*   name → the **box**
    
*   "Tushar" → the **value stored in the box**
    

Variables store data so it can be **used or changed later in the program**. ([GeeksforGeeks](https://www.geeksforgeeks.org/variables-datatypes-javascript/?utm_source=chatgpt.com))

* * *

# 2\. Declaring Variables in JavaScript

JavaScript allows three ways to create variables:

*   var
    
*   let
    
*   const
    

Example:

```plaintext
var city = "Delhi";
let age = 22;
const country = "India";
```

These keywords tell JavaScript **to reserve memory to store values**. ([CoreLangs.org](http://CoreLangs.org))

* * *

# 3\. Primitive Data Types

The **type of data stored inside a variable** is called a **data type**.

Here are the most common primitive types.

* * *

## 1\. String (text)

Used to store text.

```plaintext
let name = "Tushar";
```

Examples:

```plaintext
"Hello"
"JavaScript"
"My name is Tushar"
```

Strings represent **text data**. ([Cratecode](https://cratecode.com/info/javascript-variables-data-types?utm_source=chatgpt.com))

* * *

## 2\. Number

Used to store numbers.

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

Numbers can be:

*   integers → 10
    
*   decimals → 3.14
    

* * *

## 3\. Boolean

Boolean means **true or false**.

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

Example uses:

```plaintext
isLoggedIn = true
isAdmin = false
```

* * *

## 4\. Undefined

A variable declared but **not assigned a value** becomes undefined.

```plaintext
let score;
console.log(score);
```

Output:

```plaintext
undefined
```

* * *

## 5\. Null

null means **intentional empty value**.

```plaintext
let selectedUser = null;
```

This means:

```plaintext
The variable exists
But currently has no value
```

* * *

# 4\. Difference Between var, let, and const

These keywords behave differently.

### Example

```plaintext
var x = 10;
let y = 20;
const z = 30;
```

### Comparison Table

<table style="min-width: 100px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p>Keyword</p></td><td colspan="1" rowspan="1"><p>Can change value?</p></td><td colspan="1" rowspan="1"><p>Scope</p></td><td colspan="1" rowspan="1"><p>Notes</p></td></tr><tr><td colspan="1" rowspan="1"><p>var</p></td><td colspan="1" rowspan="1"><p>Yes</p></td><td colspan="1" rowspan="1"><p>Function scope</p></td><td colspan="1" rowspan="1"><p>Old way</p></td></tr><tr><td colspan="1" rowspan="1"><p>let</p></td><td colspan="1" rowspan="1"><p>Yes</p></td><td colspan="1" rowspan="1"><p>Block scope</p></td><td colspan="1" rowspan="1"><p>Modern JS</p></td></tr><tr><td colspan="1" rowspan="1"><p>const</p></td><td colspan="1" rowspan="1"><p>No</p></td><td colspan="1" rowspan="1"><p>Block scope</p></td><td colspan="1" rowspan="1"><p>Value cannot be reassigned</p></td></tr></tbody></table>

var variables are function-scoped, while let and const are block-scoped and preferred in modern JavaScript. ([GeeksforGeeks](https://www.geeksforgeeks.org/javascript/difference-between-var-let-and-const-keywords-in-javascript/?utm_source=chatgpt.com))

* * *

# 5\. Changing Variable Values

### Using let

Values can change.

```plaintext
let age = 20;
age = 21;
console.log(age);
```

Output

```plaintext
21
```

* * *

### Using const

Values **cannot be reassigned**.

```plaintext
const pi = 3.14;
pi = 3.1415; // Error
```

const variables must be assigned when created and cannot be reassigned later. ([techtestlog.com](http://techtestlog.com))

* * *

# 6\. What Is Scope? (Beginner Explanation)

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

Example:

```plaintext
{
let message = "Hello";

  console.log(message);

}

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

Why?

Because message only exists **inside the block { }**.

Simple idea:

```plaintext
Scope = area where a variable is accessible
```

Types (simple view):

```plaintext
Global scope → accessible everywhere
Local scope → accessible only inside a block or function
```

* * *

# Scope Visualization

```plaintext
Program
│

├── Global variables

│

└── Code block

     └── Local variables
```

* * *

# Quick Summary

<table style="min-width: 50px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p>Concept</p></td><td colspan="1" rowspan="1"><p>Meaning</p></td></tr><tr><td colspan="1" rowspan="1"><p>Variable</p></td><td colspan="1" rowspan="1"><p>A container for storing data</p></td></tr><tr><td colspan="1" rowspan="1"><p>var</p></td><td colspan="1" rowspan="1"><p>Old way of declaring variables</p></td></tr><tr><td colspan="1" rowspan="1"><p>let</p></td><td colspan="1" rowspan="1"><p>Variable that can change</p></td></tr><tr><td colspan="1" rowspan="1"><p>const</p></td><td colspan="1" rowspan="1"><p>Variable that cannot be reassigned</p></td></tr><tr><td colspan="1" rowspan="1"><p>string</p></td><td colspan="1" rowspan="1"><p>Text data</p></td></tr><tr><td colspan="1" rowspan="1"><p>number</p></td><td colspan="1" rowspan="1"><p>Numeric values</p></td></tr><tr><td colspan="1" rowspan="1"><p>boolean</p></td><td colspan="1" rowspan="1"><p>true or false</p></td></tr><tr><td colspan="1" rowspan="1"><p>null</p></td><td colspan="1" rowspan="1"><p>intentionally empty value</p></td></tr><tr><td colspan="1" rowspan="1"><p>undefined</p></td><td colspan="1" rowspan="1"><p>declared but not assigned</p></td></tr><tr><td colspan="1" rowspan="1"><p>scope</p></td><td colspan="1" rowspan="1"><p>where a variable can be used</p></td></tr></tbody></table>

* * *

💡 **Best practice in modern JavaScript**

```plaintext
Use const by default
Use let when value needs to change
Avoid var
```
