DEV Community

Cover image for JS Minute - The 6-8ish Language Types in JavaScript
Marvin Céspedes
Marvin Céspedes

Posted on

JS Minute - The 6-8ish Language Types in JavaScript

So JavaScript is weird. There are eight (sort of), ECMAScript language types in JavaScript that currently exist (at the time of this post). We tend to say that EVERYTHING in JavaScript is an object, which is not the case. In actuality, most things behave like objects in JavaScript, but that does not mean they are actual objects. Therefore, digging into what are the valid types that exist in JavaScript is an excellent thing to keep in your back pocket.

Before I continue, I want to be clear: I am referring to general value types and not the concept of primitive types - Objects would not be a part of this discussion if I were referring to primitive types and unfortunately that’s a different JS Minute for another day.

Now then, to dive into the eight-ish types and the nature of what “sort of” means we need the guidance of a helpful friend...

A drawing of a bunny wearing an eyepatch, and a beret.

Meet the BUNNSBOS!

The BUNNSBOS is going to help us make sense of these types, which are:

  • Boolean
  • Undefined
  • Number
  • Null (Sort of)
  • String
  • BigInt (unofficially)
  • Object
  • Symbol

You may question if there are other types that belong here. This is where the phrase "behave like objects" comes into play - like arrays or functions, etc. Those -as my friend puts it- are something you could classify as, "luxury objects" and worthy of a post of its own. I am only focused on the recognized types in the ECMAScript spec. 😬

So let's quickly run through these eight unique and lovely children. Warning: These are going to be extremely oversimplified definitions.

  • Boolean: Can only output either a true or false value.
  • Undefined: the Absence of value; (or in the case of a variable) a variable container that currently has no value.
  • Number: values that you can run mathematic operations on (using a floating-point format).
  • Null: Think of this as another way of intentionally writing "nothing here" - which is not the same as zero or empty. 😬😬😬
  • String: Just straightforward text.
  • BigInt: A very large number system. Can go as high as the memory allocated in the computer.
  • Object: A container for data.
  • Symbol: A function that can dynamically produce an anonymous, unique value.

Now I know you’re thinking, "Wouldn’t BUNNSSOB make more sense than BUNNSBOS? There aren't two S's in this acronym." - To which I would say, "Yeah but, look at that adorable beret!"

So we have our eight-ish types very grossly defined. I want to start chipping into the curious parts of these types - primarily BigInt and
Null. We'll start with the easy one: BigInt

BigInt is a somewhat valid type in JavaScript, or more appropriately, it exists in Node and Chrome's V8 JavaScript engine, and Firefox's SpiderMonkey engine. BigInt gets curious because it can be used and tested against but is not officially part of the ECMAScript specification. Therefore, it’s valid to use, and check for, but not officially part of the spec. Note: Depending on the version of Node or browser, you may not be able to test for BigInt, which solidifies the need for an asterisk on this type.

> typeof 143
'number'

> typeof 143n
'bigint'

Now comes the super weird one: Null. So Null can be checked against in an if statement but testing for the type of Null gets all kinds of bonkers.

> typeof null
'object'

Using a quote from frontend dev extraordinaire, Ire Aderinokun:

In the first implementation of JavaScript, values were represented in two parts - a type tag and the actual value. There were 5 type tags that could be used, and the tag for referencing an object was 0. The null value, however, was represented as the NULL pointer, which was 0x00 for most platforms. As a result of this similarity, null has the 0 type tag, which corresponds to an object.

Unfortunately, the more significant point here is that Null outputting the type of Object is a bug. A bug that we must live with lest we as a society are comfortable breaking oh so many apps and websites should we decide to fix it.

So we now carry the cross of Null - and with that cross, we must contend with the fact that we can at least check for the type of BigInt, but cannot do such a type-check with Null. Our new friend, the BUNNSBOS has died. Long live the BUNSBOS

A drawing of a butt wearing an eyepatch, and a beret.

  • Boolean
  • Undefined
  • Number
  • String
  • BigInt (unofficially)
  • Object
  • Symbol

And now, we SOB over these BUNS.

😭


Sources, credits:

Top comments (3)

Collapse
 
worc profile image
worc

Null: Think of this as another way of writing "not applicable."

i think that's a little inaccurate. or it can be. null as a concept and an implemented feature of the language can be such a pain in the ass. that said i mdn's definition is a pretty decent ideal to try to live up to:

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values.

so, in an ideal world we should trust and believe that null is intentional and there's no value there to work with. so we should be thinking of it as more "yes that variable/property exists, but there's no value here".

Collapse
 
mcespo profile image
Marvin Céspedes • Edited

That's really nice. I very much had subconsciously made my own assumptions of null without even realizing that it was a bit off. Thank you so much. I went ahead and made the edit to null on the post as a result. Thanks again.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

I would argue but absolutely wrongly that Maps and Sets should be types in JavaScript.