# what is undefined vs defined

## **1\. Undefined**

* A variable is **declared** but **not assigned any value**.
    
* JavaScript automatically assigns the value `undefined` to such variables.
    
* It means: *“The variable exists, but there is no value yet.”*
    

### Example:

```javascript
var a;
console.log(a); // undefined
```

Here, `a` **is declared**, so it exists in memory, but it has no value assigned → `undefined`.

---

## **2\. Not Defined**

* A variable is **not declared at all** in the scope.
    
* Trying to access it will cause a **ReferenceError**.
    
* It means: *“The variable does not exist in memory.”*
    

### Example:

```javascript
console.log(b); // ReferenceError: b is not defined
```

Here, `b` was never declared.

---

## **3\. Key Difference**

| Feature | **Undefined** | **Not Defined** |
| --- | --- | --- |
| Declaration status | Declared but not assigned | Never declared |
| Value | `undefined` | No value (throws error) |
| Causes error? | No | Yes → ReferenceError |

---

## **4\. Example with Both**

```javascript
var x;
console.log(x); // undefined (declared but no value)

console.log(y); // ReferenceError: y is not defined
```

---

## **5\. Relation to Hoisting**

* Variables declared with `var` are hoisted as `undefined` (not error).
    
* Variables declared with `let`/`const` are hoisted but remain in **Temporal Dead Zone (TDZ)**, so accessing before declaration → ReferenceError (similar to "not defined").
    

### Example:

```javascript
console.log(a); // undefined (because of var hoisting)
var a = 10;

console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 20;
```
