JavaScript: ‘Everything is an Object’, or is it?

A quick look at Data & Structural Types.

Jake Mills
JavaScript in Plain English

--

Photo by Ash Edmonds on Unsplash

Have you ever heard the phrase ‘everything in JavaScript is an object’? When I was first learning JS I had an instructor say this to me and it stuck. It does give you this sense of ease and a kind of security leaning on this idea when you first start out, but I’ve been thinking that this catchall phrase isn’t completely true. In fact, I dare say, it’s misleading.

This thought came to me while I was diving back into the basics of JS to uncover little nuggets of truth I may have missed during the whirlwind of my time at the Flatiron School. I wrote last week about different ways to write loops, more of a syntactical breakdown than anything else, and my train of thought this week lead me to values, aka data types. As I used the typeof operator on various values within JS I stumbled upon typeof({}); . As you would expect, if you know anything about this language, “object” was returned to the console. And for some reason this made me think of the phrase ‘everything in JavaScript is an object’. If everything in JS is an object, than why doesn’t typeof(2) return “object” instead of “number”?

Now, in the grand scheme of things, clinging to the phrase that ‘everything in JavaScript is an object’ is not going to make people turn their heads and scoff towards you (well, perhaps some, but you’re better off without people like that #goodvibesonly). It definitely helped me in the beginning as I learned JS and I continue to think of may different data/structural types as objects depending on how I am using them. But, this isn’t the beginning anymore and I’ve grown. So, how do we differentiate what is an ‘object’ and what isn’t when dealing with object-oriented programming?

Easy answer? ‘Objects refer to a data structure containing data and instructions for working with the data.’

Objects are data structures that contain data. They can be a simple key/value pair, or they can be a complex system of nested key/value pairs representing a user and all of their posts, likes, friends, comments, personal data, etc. They can store multiple values as properties. Now, I can continue down this hole and try to give a full all encompassing explanation as to what Objects are, like how Arrays are objects too, but you get the gist. In fact, I now understand why I was originally told ‘everything in JavaScript is an object’. It’s a complicated subject. Maybe it’s better to talk about primitive values, and why they aren’t objects.

JavaScript contains seven primitive data types:

Undefined (undefined), used for unintentionally missing values.

Null (null), used for intentionally missing values.

Booleans (true and false), used for logical operations.

Numbers (-100, 3.14, and others), used for math calculations.

Strings ("hello", "abracadabra", and others), used for text.

Symbols (uncommon), used to hide implementation details.

BigInts (uncommon and new), used for math on big numbers.

Dan Abramov¹

(Quick sidenote: null technically returns ‘object’ when using the typeof() operator. I was today years old when I learned that’s because of a bug in the initial version of JavaScript. Rest assured, null is a primitive value and not an object.)

A way I like to think of primitive data types is that they are considered to be fast and “light”. Objects get bogged down by holding so many different primitive types and become quite “heavy”. However, Brendan Eich knew that users of JavaScript would want to access these primitive types in different ways. Introducing primitive wrapper objects.

Developing primitive wrapper objects in JS is what provides us with different methods for some of our primitive data types. I found a great resource that explains this FAR better than I’d be able to below:

The solution looks a little bit awkward, but here it is:

1. Primitives are still primitive. A single value, as desired.
2. The language allows access to methods and properties of Strings, Numbers, Booleans and Symbols.
3. In order for that to work, a special “object wrapper” that provides the extra functionality is created, and then is destroyed.

The “object wrappers” are different for each primitive type and are called: String, Number, Boolean and Symbol. Thus, they provide different sets of methods.

For instance, there exists a string method str.toUpperCase() that returns a capitalized str.

Here’s how it works:

let str = "Hello";
alert( str.toUpperCase() ); // HELLO

Simple, right? Here’s what actually happens in str.toUpperCase():

1. The string str is a primitive. So in the moment of accessing its property, a special object is created that knows the value of the string, and has useful methods, like toUpperCase().
2. That method runs and returns a new string (shown by alert).
3. The special object is destroyed, leaving the primitive str alone.

So primitives can provide methods, but they still remain lightweight.

And that my friends is one of the reasons why you here the phrase ‘everything in JavaScript is an object’. Technically, it’s a false claim. Everything ISN’T an object in JavaScript. Maybe the phrase should be ‘everything in JavaScript can behave as an object’? Who’s to say? All I know is that the original phrase may be misleading, but it’s definitely helped me on my JS endeavors.

What do you think would be a better phrase? Let me know if this helped!

Happy Coding 🤓

Sources:

[1] Dan Abramov. “[Just JavaScript] 02. The JavaScript Universe” (Email, February 17, 2021) Just JavaScript.

--

--

Software Engineer hailing from the Empire State, writing about what interests me and hoping someone else finds it interesting too. 👨🏻‍💻 🤓 He/Him #LFGM