# Variables & Data Types in JavaScript

Yesterday, I started my JavaScript journey by understanding what JavaScript is and how it brings websites to life.

Today, I explored one of the most fundamental concepts in programming—**Variables** and **Data Types**.

Every JavaScript program, from a simple calculator to a complex web application, relies on variables to store and manage data.

Let's dive in.

* * *

# What is a Variable?

A **variable** is a named container used to store data.

Instead of writing the same value repeatedly, we store it in a variable and use it whenever needed.

For example:

```javascript
let name = "Saurabh";

console.log(name);
```

Here, `name` is the variable, and `"Saurabh"` is the value stored inside it.

* * *

# Declaring Variables

JavaScript provides three ways to declare variables:

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

* * *

## 1\. var

```javascript
var age = 22;
```

`var` is the older way of declaring variables.

It is function-scoped and allows redeclaration, which can sometimes lead to unexpected behavior.

Today, it's generally avoided in modern JavaScript.

* * *

## 2\. let

```javascript
let city = "Mumbai";
```

`let` is block-scoped and allows you to update the value later.

```javascript
let score = 10;

score = 20;
```

This is useful when the value needs to change during program execution.

* * *

## 3\. const

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

A `const` variable cannot be reassigned.

```javascript
const pi = 3.14;

// pi = 3.14159 ❌ Error
```

Use `const` whenever the value should remain constant.

* * *

# Difference Between var, let, and const

| Feature | var | let | const |
| --- | --- | --- | --- |
| Scope | Function | Block | Block |
| Redeclare | ✅ Yes | ❌ No | ❌ No |
| Reassign | ✅ Yes | ✅ Yes | ❌ No |
| Modern Usage | Rare | Common | Preferred |

In modern JavaScript, developers mainly use `let` and `const`.

* * *

# What are Data Types?

A **data type** tells JavaScript what kind of value a variable holds.

JavaScript has two categories:

*   Primitive Data Types
    
*   Non-Primitive (Reference) Data Types
    

* * *

# Primitive Data Types

## 1\. String

Stores text.

```javascript
let language = "JavaScript";
```

* * *

## 2\. Number

Stores integers and decimal values.

```javascript
let marks = 95;
let price = 99.99;
```

* * *

## 3\. Boolean

Represents true or false.

```javascript
let isLoggedIn = true;
```

* * *

## 4\. Undefined

A variable that has been declared but not assigned a value.

```javascript
let user;

console.log(user);
```

Output:

```text
undefined
```

* * *

## 5\. Null

Represents the intentional absence of a value.

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

* * *

## 6\. BigInt

Used for very large integers.

```javascript
let bigNumber = 123456789012345678901234567890n;
```

* * *

## 7\. Symbol

Used to create unique identifiers.

```javascript
const id = Symbol("userId");
```

Symbols are commonly used in advanced JavaScript applications.

* * *

# Non-Primitive Data Types

The most common non-primitive type is the **Object**.

```javascript
const student = {

name: "Saurabh",

age: 22

};
```

Arrays and functions are also objects in JavaScript.

You'll explore them in detail in upcoming lessons.

* * *

# The `typeof` Operator

JavaScript provides the `typeof` operator to check a variable's data type.

Example:

```javascript
let age = 22;

console.log(typeof age);
```

Output:

```text
number
```

More examples:

```javascript
typeof "Hello";     // string

typeof true;        // boolean

typeof undefined;   // undefined

typeof {};          // object
```

* * *

# Naming Variables

Good variable names improve code readability.

✔ Use meaningful names:

```javascript
let userName;

const totalPrice;
```

Avoid unclear names:

```javascript
let x;

let a1;
```

Meaningful names make code easier to understand and maintain.

* * *

# Best Practices

✔ Prefer `const` by default.

✔ Use `let` when the value needs to change.

✔ Avoid using `var` in modern JavaScript.

✔ Choose descriptive variable names.

✔ Use `typeof` to inspect values while learning.

* * *

# My Biggest Takeaway

Today, I realized that variables are like labeled containers that help us store and organize information in a program.

Understanding the difference between `let`, `const`, and `var`, along with JavaScript's data types, has given me a strong foundation for writing meaningful code.

Every interactive application starts by storing and working with data—and today, I learned how JavaScript handles that data.
