A toast to Javascript Prototype

Table of contents

No heading

No headings in the article.

Hello Dev! In this article, we're going to learn a fascinating aspect of JavaScript called the "Prototype Chain." It's like a family tree, but for objects in JavaScript. Let's learn this concept with some simple examples first.

Prototype Basics:

  • Every object in JavaScript has a prototype, which is another object.

  • The prototype is accessed using Object.getPrototypeOf().

const myObject = {};
const objectPrototype = Object.getPrototypeOf(myObject);

console.log(objectPrototype === Object.prototype); // true

String Prototype:

  • Strings are primitive values, but when you use methods like toUpperCase(), you're actually using the prototype.
const myString = 'Hello, World!';
const stringPrototype = Object.getPrototypeOf(myString);

console.log(stringPrototype === String.prototype); // true
console.log(String.prototype.hasOwnProperty('toUpperCase')); // true