In the beginning was the Value.
What is a value? It’s hard to say.
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.
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.
undefined—oh my! I might refer to them in my code, but they don’t exist inside my code.
“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.
Broadly, there are two kinds of values.
To see primitive values in practice, open your browser’s console and log them: