DEV Community

Cover image for JavaScript Objects Explained: From Primitives to Prototypes and Beyond
Anik Sikder
Anik Sikder

Posted on

JavaScript Objects Explained: From Primitives to Prototypes and Beyond

Hey there, fellow JavaScript adventurer! 👋

Have you ever wondered…

  • What JavaScript objects really are?
  • Why your "hello" string can call .toUpperCase() even though strings aren’t objects?
  • What on Earth is this mysterious prototype chain everyone keeps talking about?

Well, buckle up because today we’re going on a magical journey behind the scenes of JavaScript objects, primitives, and prototypes. And yes, I’ll sprinkle in some jokes because learning should never be boring!


What the Heck Is a JavaScript Object?

In JavaScript, an object is basically a collection of properties, think of it like a backpack full of labeled stuff. Each label (property name) points to something useful: a number, a string, a function, or even another object.

const user = {
  name: "Anik",
  age: 25,
  greet() {
    console.log(`Hello, my name is ${this.name}!`);
  }
};

user.greet(); // Hello, my name is Anik!
Enter fullscreen mode Exit fullscreen mode

Here, user is our backpack it has some stuff (name and age) and a little robot (greet function) that says hello.

Pro tip: You can access properties using dot notation (user.name) or bracket notation (user["name"]). Bracket notation is handy if your property name is stored in a variable or has special characters.


Is Everything in JavaScript an Object? Nope, But Almost!

JavaScript has two big camps:

1. Primitives: The Simple Folk

  • Strings ("hello")
  • Numbers (42)
  • Booleans (true/false)
  • null and undefined (the mysterious ghost values)
  • Symbols (unique little tags)
  • BigInts (huge numbers)

Primitives are not objects. They’re simple values with no built-in properties or methods.

2. Objects: The Fancy Ones

  • Arrays ([1, 2, 3])
  • Functions (yep, functions are objects!)
  • Dates
  • Your own custom objects

Objects can have properties and methods attached directly.

Why care? Because understanding the difference helps avoid bugs for example, primitives are immutable, so you can’t change them directly.


But Wait, How Can Primitives Call Methods Then?

If primitives aren’t objects, how can you do this?

"hello".toUpperCase(); // "HELLO"
Enter fullscreen mode Exit fullscreen mode

JavaScript’s Sneaky Trick: Wrapper Objects

JavaScript is clever. When you call a method on a primitive, it wraps it in an object temporarily. For strings, it creates a String object, for numbers a Number object, and so on.

It’s like putting on a disguise just long enough to use a method:

"hello".toUpperCase(); 
// Behind the scenes: new String("hello").toUpperCase()
Enter fullscreen mode Exit fullscreen mode

After the method runs, the disguise (wrapper object) disappears. Magic!

Just remember: this doesn’t work for null or undefined. Trying to call a method on those will throw a tantrum (error).


The Prototype Chain: Your Object’s Family Tree

Every object has a secret family, its prototype. When you ask an object for something it doesn’t have, it checks with its prototype. If the prototype doesn’t have it, it asks its prototype’s prototype, and so on… until it either finds it or hits the top (which is Object.prototype).

Picture it like this: You lost your keys.

  • You check your backpack first (the object itself). No keys.
  • You ask your parent (prototype). Nope.
  • You ask your grandparent (prototype’s prototype). Still no luck.
  • Eventually, you give up (end of prototype chain).

This is how JavaScript looks up properties and methods.

Example:

let arr = [1, 2, 3];
arr.push(4); // You didn’t see push on arr, but on Array.prototype
console.log(arr); // [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

What Exactly Is __proto__ and Object.getPrototypeOf()?

You might have seen the mysterious __proto__ property floating around. It’s a way to access an object’s prototype, but it’s considered legacy and better avoided in favor of:

Object.getPrototypeOf(obj);
Enter fullscreen mode Exit fullscreen mode

Here’s how you can check an object’s prototype chain step-by-step:

const obj = {};
console.log(Object.getPrototypeOf(obj) === Object.prototype); // true
Enter fullscreen mode Exit fullscreen mode

Fun fact: The prototype chain always ends with null:

console.log(Object.getPrototypeOf(Object.prototype)); // null
Enter fullscreen mode Exit fullscreen mode

Classes? Just Fancy Prototypes in Disguise

ES6 classes are just syntactic sugar over prototypes. They make it easier to write constructor functions and methods.

class Person {
  constructor(name) {
    this.name = name;
  }

  greet() {
    console.log(`Hi, I'm ${this.name}`);
  }
}

const anik = new Person("Anik");
anik.greet(); // Hi, I'm Anik
Enter fullscreen mode Exit fullscreen mode

Under the hood, greet lives on Person.prototype shared by all Person instances. Efficient and memory-friendly!


Why Do Prototypes Matter?

Imagine if every object had its own copy of greet. Your program would be a memory hog!

Thanks to prototypes, methods live once and everyone shares them like sharing one Netflix account instead of buying individual subscriptions. Savings for everyone!


What About Functions? They’re Objects Too!

In JavaScript, functions are special objects. They have properties and can have prototypes too.

This is why you can do things like:

function sayHi() {
  console.log("Hi!");
}

sayHi.language = "English";

console.log(sayHi.language); // English
Enter fullscreen mode Exit fullscreen mode

Plus, functions have a prototype property (used when functions act as constructors).


Quick Recap: Your JavaScript Object Cheat Sheet

  • Primitive Types: Simple values without methods or properties
  • Wrapper Objects: Temporary objects that let primitives use methods
  • Objects: Collections of properties and methods
  • Prototype Chain: A chain of objects for looking up missing properties
  • Classes: Cleaner syntax for prototype-based inheritance
  • Functions: Special objects that can have properties and prototypes

Final Words: You’re Now a JavaScript Object Wizard! 🧙‍♂️

Understanding objects, primitives, and prototypes gives you superpowers in JavaScript. Next time you call "hello".toUpperCase() or create a new object, you’ll know exactly what’s going on behind the scenes.


Before You Go, Here’s a Little JavaScript Humor

Why don’t JavaScript developers like working with prototypes?
Because they don’t want to duplicate work! 😆


If you enjoyed this, share it with your fellow devs and follow me for more JavaScript wizardry! 🪄

Top comments (0)