The JavaScript Universe
In the beginning was the Value.
What is a value? It’s hard to say.
What is a point in geometry? What is a word in human language? A value is a fundamental concept in JavaScript—so we can’t define it through other terms.
Instead, we’ll define it through examples. Numbers and strings are values. Objects and functions are values, too.
There are also a lot of things that are not values, like the pieces of our code—our if
statements, loops, and variable declarations, for example.
Values and Code
As we start building our mental model, one of the first common misconceptions we need to clear up is that values are our code. Instead, we need to think of them separately—our code interacts with values, but values exist in a completely separate space.
To distinguish between values and code in my JavaScript program, I like to imagine this drawing of the Little Prince by Antoine de Saint-Exupéry:
I’m standing on a small planet, holding a list of instructions. This list is my program—my code. As I read through my list of instructions, I can see a lot going on—there are if
statements, variable declarations, commas and curly braces.
My code contains instructions like “make a function call,” “do this thing many times,” or even “throw an error.” I read through these instructions step by step from the surface of my little world.
But every once in a while, I look up.
On a clear night, I see the different values in the JavaScript sky: booleans, numbers, strings, symbols, functions and objects, null
and undefined
—oh my! I might refer to them in my code, but they don’t exist inside my code.
In our JavaScript universe, values float in space.
“Hold on,“ you might say, “I always thought of values as being inside of my code!” Here, I’m asking you to take a leap of faith. It will take a few more modules for this mental model to pay off. Give it five minutes. I know what I’m doing.
Values
Broadly, there are two kinds of values.
Primitive Values
Primitive values are like stars—cold and distant, but always there when I need them. Even from the surface of my small planet, I can find them and point them out. They can be numbers and strings, among other things. All primitive values have something in common: They are a permanent part of our JavaScript universe. I can point to them, but I can’t create, destroy, or change them.
To see primitive values in practice, open your browser’s console and log them:
console.log(2);console.log('hello');console.log(undefined);
These might seem like small steps, but we’re laying the foundation for everything else to come. We’re building our JavaScript universe, together. There are 9 more chapters with exercises for you to practice.