Everything in JavaScript is an Object

A confusing concept when you begin learning JavaScript is the notion that everything is an object. How is that possible? How is a string an object? How is an array an object? Everything in JavaScript is an object is a difficult concept to wrap your head around… That is, until you learn about and understand prototypes. Prototypes, while having a scary name, are a relatively simple concept. They are just JavaScript’s way of handling inheritance. And once you wrap your head around prototypes and prototypal inheritance, you will understand why everything in JavaScript is an object. Strings, functions, arrays and numbers all inherit the object prototype. That is the reason why you can call object methods on each of these types in addition to their own respective methods. Everything in JavaScript inherits the object prototype. That is why “Everything in JavaScript is an object”.

Below are a couple links about JavaScript prototypes that I found helpful.

Posted by: John Dugan


  • Shmuli

    This information is incorrect; not everything is an object in JavaScript. Simple primitive types (string, boolean, number, null and undefined) are not objects in their natural default state. For instance, the primitive value “Hello, world!” is a primitive literal and immutable value. Our ability to perform operations on this string, like checking its length (getting and setting properties and performing a series of String methods), is only due to the fact that the JavaScript engine automatically coerces the primitive type string into a String object when necessary (this is commonly referred to as “auto-boxing”). Same with numbers. In order to be an object it needs to be created using a constructor. Our ability to perform operations on the primitive number type is because the JavaScript engine converts it on the fly when we perform a method that requires object form. Only Objects, Arrays, Functions, and RegExp are actually objects regardless of how they are created (whether in literal form or using a constructor method). Date and Error are objects, as they are only created through construction form.

  • Shmuli

    You mentioned in one of your comments (on “Object Oriented JavaScript Pattern Comparison) that you are a big fan of Kyle Simpson’s OLOO pattern. This point is addressed at length in the same book where he presents his OLOO pattern.

  • Hi Shmuli,

    First off, thank you for taking the time to write such a great comment. You are 100% correct. What I have presented is at best misleading and at worst flat out wrong. I’ve been meaning to update this post – just have not had the time.

    RE OLOO – I was introduced to OLOO in one of Kyle’s workshops. I have not yet read the YDKJS chapter where it is formally presented. The explanation of OLOO in that post is very basic.

  • Shmuli

    It was actually a search for OLOO which brought me to your blog. I was looking for more information on the specific design choices in Kyle’s Example. As it seems to me though, the OLOO (Objects Linking to Other Objects) pattern is not a new idea at all. It’s simply an old idea (the Prototype pattern), with a new name. Kyle coins it by that name to make the distinction that you don’t need to mimic classical inheritance (copying nature) in JavaScript, and that by using the protoypal inheritance (linking nature) that’s native to the language, you don’t have to jump through hoops to accomplish object-oriented design. Do you agree with that, or does Kyle’s OLOO pattern actually differ from the prototype pattern in some degree?

  • While OLOO certainly enjoys the benefits of prototypal inheritance (or “delegation” as Kyle would say), that’s not what distinguishes the pattern. As you said, OO patterns that take advantage of prototypal inheritance are nothing new.

    The key aspect of OLOO is the omission of the new operator. OLOO bypasses the constructor function and creates objects directly from other objects. For my small brain, this massively simplifies things – it’s why I am a big fan of the pattern.

    I stitched together a few of Kyle’s slides to better communicate the benefits of OLOO: https://john-dugan.com/wp-content/uploads/2015/04/oloo-slides.pdf

    Let me know if this helps 🙂