Lara

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

原文链接: dev.to

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: