- Define the Variable Lookup Expression
The final of our essential three expressions is the variable lookup expression. Like the constant expression, it is boring yet profound. Once we've assigned a value to a variable name using the assignment expression, we can use the variable lookup expression to retrieve that value. Using our metaphors from the previous lesson, it looks up the variable's definition in the dictionary, or "shakes out" the value that was put in the labeled box.
To look up the value in a variable we simply type the variable's name in.
// Assignment expression that returns 32
age = 32;
// Type in the assigned name
age;
In return, we get 32
. Try this out on your own in the below REPL console.
Remember, the more you try things out, the more it will start to make sense!
That's it. The values we associate with the assignment expression can be retrieved by simply typing the variable's name.
In the previous lesson, we talked about a parent and a baby. A parent repeats
their name hundreds of times to get the baby to assign their face to the
variable ma-ma
. When the baby first sees that face again and says "ma-ma
!"
the parent has successfully taught the child "variable lookup." Although there
are no pages in baby journals for "Baby's First Variable Lookup."
If you think about it, most of childhood education until early elementary school is giving them thousands of assignment expressions so they can participate in the world: "Red," "one," "eleven," "far."
Consider this scenario. Look for the essential three expressions in here.
Parent: See the doggie? That's a doggie.
Child: Doggie?
Parent: That's right, that's a doggie. Doggies say "Woof-woof!"
Child: Doggie?
Parent: Right. Doggie.
Some time later...
Child: Doggie!
Parent: That's right. Doggie!
Let's compare teaching a baby a four-legged animal's name and teaching JavaScript
that a
is 4
.
Now that the essential three expressions are under your control, we're going to start rapidly building up the richness of things you can do using expressions. Whole programming languages are built around executing through evaluating expressions! Languages that work this way are called "functional languages" and they are some of the first programming languages ever created.