Values, Statements and Expressions


Values, Statements and Expressions

As a fledgling programmer, one of your prime directives is learning to think like a computer, or more specifically learning to understand what your computer expects.

An important feature of any modern programming language is the ability to create values. There are two fundamental ways to create values - statements and expressions.

Before diving in, let’s start by taking a step back and looking at the big picture. Though it may not be immediately apparent, I promise this brief detour will become relevant in the end, when we start talking expressions and statements.

  • Everything a computer does, it does because of software.

  • Software consists of programs (otherwise known as applications).

  • In order for a program to do anything at all, that program must provide values for the computer to manipulate, and tell it how to manipulate them.

Programming, then, amounts to nothing more than writing instructions for a computer to follow.

These instructions primarily consist of

  • values - corresponding to words, numbers, URLs, files on your hard drive, ASCII codes for rendering text, etc


  • operations - actions to be performed on those values.

In their earliest stages, computers did this in a very rudimentary way, and were mostly reserved for performing complex mathematical operations.

Fortunately, computers have become sophisticated enough that we’re no longer restricted to using convoluted machine-level operations to write our programs.

Nevertheless, the fundamental mechanisms are still very much the same. We give values to the computer and instruct it to perform actions. Underneath the surface of your operating system’s sleek, user-friendly UI, there are programs running all the time, each responsible for keeping your computer functioning properly (hopefully).

If you’re on macOS / Linux, open your terminal and type top to see what I’m talking about. Windows users can open Task Manager and switch to the Processes tab.

Hopefully this will set you up to grasp (what was for me anyway) a critical takeaway in thinking about my programs: all the magic of computers - that is, all programs and applications - can ultimately be reduced to the creation and manipulation of values.

Now I’ll get to the point.

In JavaScript, a value is created in one of two fundamental ways - with statements, or with expressions.

The key distinction between the two is how you will arrive at that value.

Consider this:

When we declare a statement - ‘declare’ being the proper computer science-y term - we’re making a self-evident claim about a value, with the intent of later performing some kind of action upon it.

Open your console in the Chrome DevTools - or start a REPL by running node in your terminal - declare a variable, and watch what happens:

> var a = 1;

Why undefined? I mean, we just defined a variable, didn’t we?

It’s true - we have declared a variable a, and set it to 1. But we didn’t do anything beyond that.

The REPL merely Reads your input, Evaluates it, Prints any returned value, and then Loops back around, resetting your prompt. In this case, it didn’t arrive at any return value, so it prints a default: undefined.

If we want to see the value of a, we have to explicitly ask for it - or rather, call it.

> var a = 1;
> a

If you imagine that you and JavaScript are having a conversation, declaring the statement var a = 1; is like you saying,

“Let’s set the Variable a to a Number value of 1.”

JS hears you, nods, makes a mental note of it, but doesn’t have anything to respond with.

But when you call the variable with a, it’s as if you’re asking a question:

“Can you give me that a thing I mentioned earlier?”

JavaScript thinks to itself,

“Oh yeah, a. I have a value for that.”

and replies back:


a, then is our first expression:

“Expressions are a combination of one or more explicit values, constants, variables, operators, and functions that the programming language interprets…and computes to produce (“to return”, in a stateful environment) another value. (from wikipedia)

When you call a with the variable expression “a”, the JavaScript engine interprets that expression: it sees that you are asking for the variable a, then computes it - it goes and looks for a value associated with a variable called a.

Reload your browser (or close and re-open your node REPL). Now try calling a without defining it:

> a
Uncaught ReferenceError: a is not defined

Notice we didn’t simply get undefined as before. Why?

Let’s go one step further before getting into it. Define a variable a, but don’t set it to anything:

> a
Uncaught ReferenceError: a is not defined
> var a;
> a

On our first attempt to call a, the JS REPL Reads the input and Evaluates it, but it doesn’t find any mention of a, because a hasn’t been set to any value.

When you call a variable that hasn’t been set, the JavaScript engine is programmed to print a ReferenceError. That’s JavaScript’s way of letting you know that a isn’t set to anything.

The takeaway here is that when you call a - regardless of whether or not you’ve defined it - you are writing an expression. Or rather, the act of calling a is an expression. And expressions, as we’ve seen, are interpreted and computed to produce value.

That’s why we can declare a in a statement without setting it to anything, call it with the expression a, and get a value back (not an error).

Again, it’s as if we’re just saying out loud,

“Let’s create a variable called a.”

JavaScript nods in acknowledgement. Then we ask,

“Can you give me the value of a?”

Javascript thinks back for a moment and says,

“Sure. a has a value, but it’s just undefined.”

After all, undefined is a value too. The authors of JavaScript created undefined to represent the value of variables that have been named, but not set. Without it, we’d have a much harder time debugging our applications.

That’s pretty much it for expressions and statements! If this is all a little fuzzy right now, don’t panic. These concepts will become much more intuitive as you begin exploring more complex features of the language.