Just JavaScript· 02
Chapter 02 · Foundations

The JavaScript
Universe

In the beginning was the Value. Values don't live inside your code — they float in space, and your code points up at them. There are two families and exactly nine types. After twenty-five years, that's all anyone's found.

Lab · map a value to its type

Point at a value, see where it lives

Click any value. The console answers typeof with one of nine strings — and we place it in the universe: distant stars (primitives) on the outer ring, nearby rocks (objects & functions) on the inner one. Watch the surprises.

typeof( … )
pick a value →
01

Code, and then values

Picture standing on a tiny asteroid — that's your code. You look up and see the values floating in the sky. You can refer to them, but they don't live in your code.

On the asteroid's surface are the if statements, the commas, the curly braces — the instructions you walk through step by step. But every so often you look up, and there in the JavaScript sky are the values: booleans, numbers, strings, symbols, functions, objects, null and undefined. You might refer to them from your code, but they exist out there, not inside it. Hold this as a leap of faith — it pays off over the next few chapters.

Two families of value

Primitive values — numbers, strings, and a few others — are like cold, distant stars. There is nothing you can do in your code to change them; they're always there when you need them. Objects and functions are different: they float close to your code, near enough that you can manipulate them and even make more of them.

Expressions are questions

Some questions JavaScript will happily answer. Those are expressions. Ask 2 + 2 and it answers with the value 4. An expression always results in a single value — and typeof(x) is itself an expression whose value is a string like "number".

02

The nine types — and one urban legend

Two of these are "lonely": null and undefined are the only values of their types. They'll cause trouble later.

familytypeused fortypeof
primitiveUndefinedunintentionally missing values"undefined"
primitiveNullintentionally missing values"object"
primitiveBooleanlogic"boolean"
primitiveNumbermath"number"
primitiveStringtext"string"
primitiveSymbolhiding implementation detail"symbol"
primitiveBigIntmath on big integers"bigint"
non-primitiveObjectgrouping data"object"
non-primitiveFunctionreferring to code"function"

"But what about arrays?" There are no other fundamental types — arrays, dates, and regular expressions are all objects. And the famous claim that "everything is an object" is an urban legend: "hi".toUpperCase() only seems object-like because JavaScript briefly wraps the string in a temporary object and then throws it away. Primitives are not objects.

Think first

One of these results surprises most people. Which, and why?

typeof(null)        // ?
typeof([])          // ?
typeof(x => x * 2) // ?
Answer

typeof(null) is "object" — a historical bug we're stuck with forever; null is genuinely a primitive of its own type. typeof([]) is "object" because arrays are objects. typeof(x => x * 2) is "function" — functions get their own answer even though, under the hood, they're a special kind of object.

03

Recap

  • There are values, and there's everything else. Values float in the universe; your code refers to them but doesn't contain them.
  • Two families: primitives (distant, unchangeable stars) and objects & functions (nearby rocks you can manipulate). Nine types total.
  • null and undefined are each the only value of their type — the troublemakers.
  • Ask questions with expressions; JavaScript answers with values. typeof(x) answers with a type string. Not everything is an object.