# How Everything is an Object in JavaScript (Prototype Explained)

If you’ve been learning JavaScript, you’ve probably heard:

> “Everything in JavaScript is an object”

* * *

### 1\. The Real Core Concept: Prototype Chain

Let’s start with a simple example:

![](https://cdn.hashnode.com/uploads/covers/624226a5db84f8c50fa5b247/0733481b-1aa1-4cb1-8248-f5fe4d0c6956.png align="left")

```javascript
const user1 = {
    name: 'Shubham',
}

const user2 = {
    __proto__: user1
}

console.log(user2) // {}
console.log(user2.name) // 'Shubham'
```

**What’s happening behind the scenes?**

```js
user2
  ↓ (__proto__)
user1
  ↓ (__proto__)
Object.prototype
  ↓
null
```

**Flow when accessing** `user2.name`

```plaintext
Step 1: Check user2 → not found
Step 2: Go to user1 → found ("shubham")
Step 3: Stop
```

This is called **Prototype Chain**

* * *

### Key Insight

> Objects in JavaScript don’t inherit by copying—they inherit by linking i.e. objects are linked using the prototype (`__proto__`). When a property is accessed on an object, JavaScript first looks for it in the object itself. If not found, it automatically looks up the prototype chain until it finds the property or reaches `null`.

* * *

### 2\. Arrays Are Also Objects

```javascript
let arr = [1, 2, 3];
arr.push(4);
```

Looks like a special data structure, right?

But internally

```js
arr (Array instance)
  ↓ (__proto__)
Array.prototype
  ↓ (__proto__)
Object.prototype
  ↓
null
```

**What this means:**

*   `arr.push()` → comes from `Array.prototype`
    
*   `arr.toString()` → comes from `Object.prototype`
    

So arrays is also an **objects.**

* * *

### 3\. Strings: The Interesting Case

```javascript
let name = "shubham";
console.log(name.toUpperCase());
```

**But string is primitive… right?**

YES  
  
But JS does **auto-boxing**

**What actually happens behind the scenes:**

```javascript
"shubham" (primitive)
   ↓ (auto-boxing)
new String("shubham")
   ↓
String.prototype
   ↓
Object.prototype
   ↓
  null
```

```plaintext
Step 1: "shubham".toUpperCase()
Step 2: JS converts → String object
Step 3: Finds method in String.prototype
```

* * *

### Key Insight

> Primitives are not objects, but JavaScript makes them behave like objects when needed.

* * *

### 4\. Function Example

```javascript
function test() {}

test.myName = "shubham"; //Only objects can store key-value pairs → so test is an object

console.log(test.myName); // shubham 

console.log(test.call);   // function
console.log(test.apply);  // function
console.log(test.bind);   // function

console.log(test.__proto__ === Function.prototype); // true
console.log(Function.prototype.__proto__ === Object.prototype); // true
```

```js
test (function object)
  ↓ (__proto__)
Function.prototype
  ↓ (__proto__)
Object.prototype
  ↓
null
```

* * *

### Final Conclusion

*   Objects use **prototype chaining**
    
*   Arrays & functions are objects
    
*   Primitives can behave like objects via **auto-boxing**
    

> JavaScript is not class-based, it is **prototype-based** because objects inherit properties and methods from other objects through the prototype chain.
