Skip to main content

Command Palette

Search for a command to run...

How Everything is an Object in JavaScript (Prototype Explained)

Updated
3 min read
How Everything is an Object in JavaScript (Prototype Explained)
S

I'm Shubham (@shubhamsinghbundela), I'm a Software Engineer, a Full-stack developer, a tech enthusiast, and a technical writer here on @Hashnode. I have a strong zeal to share my acquired knowledge and I am also willing to learn from others.

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:

const user1 = {
    name: 'Shubham',
}

const user2 = {
    __proto__: user1
}

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

What’s happening behind the scenes?

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

Flow when accessing user2.name

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

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

Looks like a special data structure, right?

But internally

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

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

But string is primitive… right?

YES

But JS does auto-boxing

What actually happens behind the scenes:

"shubham" (primitive)
   ↓ (auto-boxing)
new String("shubham")
   ↓
String.prototype
   ↓
Object.prototype
   ↓
  null
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

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
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.

More from this blog

Shubham Tech. Blog's

55 posts

Problem Solver | Currently Working As a Full Stack Developer, Community Leader At @Dev_Matrix | Previously Contributor at @RealDevSquad, @TeamShiksha