Member-only story
Inline: a series about owning your business logic
JavaScript Type Names
Living with the type system we love to hate.
This is part of the Inline series, a series of copy-paste code solutions to reduce our dependency on third-party packages. Please do review the About page before reading or commenting.
Skill level: beginner
If you do not have a Medium subscription, please do read the free version here.
The problem space
We are going to be talking about types quite a bit in this series. While I think JavaScript’s dynamic type system is one of its superpowers, I also think its implemented has some gaps. One such gap is in type names.
The primary language feature for type names in JavaScript is the typeof expression. Unfortunately, it does not provide a consistent experience. That’s HR speak for “it kind of sucks”. Here’s what it gives us:
typeof undefined // 'undefined'
typeof null // 'object'
typeof 'foo' // 'string'
typeof new String('foo') // 'object'
typeof {} // 'object'
typeof [] // 'object'
typeof 42 // 'number'
typeof true // 'boolean
typeof /foo/g // 'object'…








