Skip to content

Studying from the Inside

episode 04

In the next module, we’ll take a closer look at our JavaScript universe and the values in it. But before we can get to that, we need to address the elephant in the room. Is our JavaScript universe even real?

The JavaScript Simulation

I live on my small planet inside our JavaScript universe.

When I ask our universe a question, it answers with a value. I certainly didn’t come up with all these values myself. The variables, the expressions, the values—they populate our world. The JavaScript world around me is absolutely real to me.

But sometimes, there is a moment of silence before the next line of code; a brief pause before the next function call. During those moments, I see visions of a world that’s much bigger than our JavaScript universe.

In these visions, there are no variables and values; no expressions and no literals. Instead, there are quarks. There are atoms and electrons. There’s water and life. Perhaps you’re familiar with this universe, too?

There, sentient beings called “humans” use special machines called “computers” to simulate our JavaScript universe. Some of them do it for amusement. Some of them do it for profit. Some of them do it for no reason at all. At their whim, our whole universe is created and destroyed a trillion times a day.

Maybe our JavaScript universe isn’t so real, after all.

Knowing this, we can study our world in two ways.

Studying From the Outside

One way to study our JavaScript universe would be to study it from the outside.

We might focus on how a simulation of our world—a JavaScript engine—“really” works. For example, we might learn that this string of text—a value in our world—is a sequence of bytes stored inside a silicon chip.

This approach puts our mental focus on the physical world of people and computers. Our approach is different.

Studying From the Inside

We will be studying our world from the inside. Transport yourself mentally into the JavaScript universe and stand next to me. We will observe our universe’s laws and perform experiments like physicists in the physical universe.

We will learn about our JavaScript world without thinking about how it’s implemented. This is similar to a physicist discussing the properties of a star without questioning whether the physical world is real. It doesn’t matter! Whether we’re studying the physical or the JavaScript universe, we can describe them on their own terms.

Our mental model will not attempt to explain how a value is represented in the computer’s memory. The answer changes all the time! The answer even changes while your program is running. If you’ve heard a simple explanation about how JavaScript “really” represents numbers, strings, or objects in memory, it is most likely wrong.

To us, each string is a value—not a “pointer” or a “memory address”—a value. In our universe, a value is good enough. Don’t allow “memory cells” and other low-level metaphors to distract you from building an accurate high-level mental model of JavaScript. It’s turtles all the way down, anyway!

If you’re coming from a lower-level language, set aside your intuitions about “passing by reference,” “allocating on stack,” “copying on write,” and so on. These models of how a computer works often make it harder to be confident in what can or cannot happen in JavaScript. We’ll look at some of the lower-level details, but only where it really matters. They can serve as an addition to our mental model, rather than its foundation.

The foundation of our mental model is values. Each value belongs to a type. Primitive values are immutable. We can point to values using “wires” we call variables. This foundation—this understanding of values—will help us continue building our mental model.

As for these strange visions, I don’t give them much thought anymore. I have wires to point and questions to ask. I better get to it!

The stars are bright when I look at them.

Are they still there when I blink?

I shrug.

“Implementation details.”