# JavaScript Revision Part 1 – Variables & Data Types

JavaScript variables and data types form the foundation of the language. Understanding how values are stored, copied, and identified is essential for writing efficient code and answering common interview questions.

This guide provides a quick revision of the most important concepts with concise explanations and practical examples.

## Topics Covered

*   Variables
    
*   `var`
    
*   `let`
    
*   `const`
    
*   Primitive Data Types
    
*   Non-Primitive Data Types
    
*   Pass by Value vs Pass by Reference
    
*   `undefined` vs `null`
    
*   `undefined` vs `not defined`
    
*   `NaN`
    
*   `typeof` Operator
    

* * *

## Variables

### Definition

A variable is a named container used to store data in memory so it can be accessed, updated, and reused throughout a program.

### Explanation

Instead of using hardcoded values multiple times, we store them in variables. JavaScript is a dynamically typed language, so a variable can hold any type of value.

### Example

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

### Important Points

*   Used to store data.
    
*   Can hold any data type.
    
*   Makes code reusable and maintainable.
    

* * *

## var

### Definition

`var` is the original way of declaring variables in JavaScript. It is **function-scoped**, can be redeclared, and can be reassigned.

### Explanation

Before ES6, developers used `var` for every variable. Since it ignores block scope and allows redeclaration, it can lead to unexpected behavior. For this reason, it is rarely used in modern JavaScript.

### Example

```javascript
var age = 20;

var age = 25;

age = 30;
```

### Important Points

*   Function scoped
    
*   Can be redeclared
    
*   Can be reassigned
    
*   Hoisted and initialized with `undefined`
    
*   Avoid using `var` in modern JavaScript
    

* * *

## let

### Definition

`let` is a block-scoped variable introduced in ES6. It allows reassignment but does not allow redeclaration within the same scope.

### Explanation

Unlike `var`, `let` respects block scope, making it safer to use inside loops, conditions, and functions.

### Example

```javascript
let age = 20;

age = 21;
```

```javascript
let age = 20;
let age = 25; // SyntaxError
```

### Important Points

*   Block scoped
    
*   Cannot be redeclared
    
*   Can be reassigned
    
*   Hoisted but remains in the Temporal Dead Zone until initialization
    

* * *

## const

### Definition

`const` declares a block-scoped variable whose reference cannot be reassigned after initialization.

### Explanation

Use `const` for values that should not be reassigned. For objects and arrays, only the reference is constant, not the contents.

### Example

```javascript
const user = {
  name: "Aditya",
};

user.name = "Rahul"; // Valid

user = {}; // Error
```

### Important Points

*   Block scoped
    
*   Cannot be redeclared
    
*   Cannot be reassigned
    
*   Must be initialized during declaration
    
*   Object and array contents can still be modified
    

* * *

## var vs let vs const

| Feature | var | let | const |
| --- | --- | --- | --- |
| Scope | Function | Block | Block |
| Redeclare | Yes | No | No |
| Reassign | Yes | Yes | No |
| Hoisted | Yes | Yes | Yes |
| Temporal Dead Zone | No | Yes | Yes |

* * *

## Primitive Data Types

### Definition

Primitive data types are immutable values that store the actual value directly.

### Explanation

Primitive values are copied when assigned to another variable. Modifying one variable does not affect the other because each variable stores its own copy.

JavaScript has **7 primitive data types**:

*   String
    
*   Number
    
*   Boolean
    
*   Undefined
    
*   Null
    
*   Symbol
    
*   BigInt
    

### Example

```javascript
let a = 10;
let b = a;

b = 20;

console.log(a);
```

Output

```text
10
```

### Important Points

*   Immutable
    
*   Stored by value
    
*   Compared by value
    
*   Independent copies are created during assignment
    

* * *

## Non-Primitive Data Types

### Definition

Non-primitive data types are objects that store a reference to the actual data instead of the data itself.

### Explanation

When an object or array is assigned to another variable, JavaScript copies the reference, not the object. Both variables point to the same memory location.

Examples include:

*   Object
    
*   Array
    
*   Function
    
*   Date
    
*   Map
    
*   Set
    

### Example

```javascript
const user1 = {
  name: "Aditya",
};

const user2 = user1;

user2.name = "Rahul";

console.log(user1.name);
```

Output

```text
Rahul
```

### Important Points

*   Mutable
    
*   Stored by reference
    
*   Compared by reference
    
*   Multiple variables can reference the same object
    

* * *

## Pass by Value vs Pass by Reference

### Pass by Value

#### Definition

Pass by value means a copy of the original value is assigned or passed.

#### Explanation

Changing the copied value does not affect the original because both variables store separate copies.

#### Example

```javascript
let a = 10;
let b = a;

b = 20;

console.log(a);
```

Output

```text
10
```

* * *

### Pass by Reference

#### Definition

For objects, JavaScript copies the reference to the object instead of creating a new object.

#### Explanation

Both variables point to the same object. Any modification made through one variable is visible through the other.

#### Example

```javascript
const obj1 = {
  name: "John",
};

const obj2 = obj1;

obj2.name = "Alex";

console.log(obj1.name);
```

Output

```text
Alex
```

### Key Differences

| Feature | Pass by Value | Pass by Reference |
| --- | --- | --- |
| Applies To | Primitive Types | Objects |
| Copy Created | Yes | No |
| Memory Shared | No | Yes |
| Original Value Changes | No | Yes |

> **Interview Note:** JavaScript is technically **pass-by-value**. For objects, the value being copied is the reference, which is why changes are reflected in all variables pointing to the same object.

* * *

## Undefined vs Null

### Undefined

#### Definition

`undefined` is the default value assigned to a variable that has been declared but not initialized.

#### Example

```javascript
let age;

console.log(age);
```

Output

```text
undefined
```

### Null

#### Definition

`null` represents an intentional absence of value assigned by the developer.

#### Example

```javascript
let user = null;
```

### Key Differences

| Undefined | Null |
| --- | --- |
| Assigned by JavaScript | Assigned by Developer |
| Variable has no value yet | Variable intentionally has no value |
| Type is `undefined` | `typeof null` returns `"object"` |

## Undefined vs Not Defined

### Undefined

A variable exists but has not been assigned a value.

```javascript
let city;

console.log(city);
```

Output

```text
undefined
```

* * *

### Not Defined

A variable has never been declared.

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

Output

```text
ReferenceError: city is not defined
```

### Key Differences

| Undefined | Not Defined |
| --- | --- |
| Variable exists | Variable doesn't exist |
| Returns `undefined` | Throws `ReferenceError` |

## NaN

### Definition

`NaN` stands for **Not a Number** and represents an invalid numeric result.

### Explanation

It is returned when a mathematical operation cannot produce a valid number.

### Example

```javascript
console.log(Number("Hello"));
```

Output

```text
NaN
```

Another common interview question:

```javascript
console.log(NaN === NaN);
```

Output

```text
false
```

Use `Number.isNaN()` to check whether a value is `NaN`.

### Important Points

*   Type of `NaN` is `number`
    
*   `NaN` is not equal to itself
    
*   Use `Number.isNaN()` instead of `==` or `===`
    

* * *

## typeof Operator

### Definition

The `typeof` operator is used to determine the data type of a value.

### Explanation

It returns a string representing the type of the operand. It is commonly used for type checking in JavaScript.

### Example

```javascript
typeof "Hello";      // "string"
typeof 10;           // "number"
typeof true;         // "boolean"
typeof undefined;    // "undefined"
typeof Symbol();     // "symbol"
typeof 10n;          // "bigint"
typeof {};           // "object"
typeof [];           // "object"
typeof function(){}; // "function"
typeof null;         // "object"
```

### Important Points

*   Returns the type as a string.
    
*   Arrays return `"object"`.
    
*   Functions return `"function"`.
    
*   `typeof null` returns `"object"` due to a legacy JavaScript behavior.
    

## Interview Questions

1.  What is the difference between `var`, `let`, and `const`?
    
2.  What is the difference between function scope and block scope?
    
3.  Explain primitive and non-primitive data types.
    
4.  What is the difference between pass by value and pass by reference?
    
5.  What is the difference between `undefined` and `null`?
    
6.  What is the difference between `undefined` and `not defined`?
    
7.  What is `NaN`? Why is `NaN !== NaN`?
    
8.  Why does `typeof null` return `"object"`?
    
9.  Why does `typeof []` return `"object"`?
    
10.  How does the `typeof` operator work?
