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...
Meet the BUNNSBOS!
BUNNSBOS is going to help us make sense of these types, which are:
- Null (Sort of)
- BigInt (unofficially)
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
Null. We'll start with the easy one:
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 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:
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
- BigInt (unofficially)
And now, we SOB over these BUNS.