Second Big Idea: Abstraction Through Numbers

November 27th, 2022

The second big idea is the concept of thinking about the real world in terms of numbers. You’ve been doing this since you were a kid. A car going 60 mph is going faster than a car going 20 mph. $30 for a hamburger is too expensive. Your computer has only 4 GB of memory; it would work better if it had 8 GB. And that soft serve ice cream cone will fatten you with 207 calories — dirty ricklefricks!

We use numbers all the time, but we also think about numbers in more abstract terms. That is, we can see beyond what a number actually is and think about what it might be. Imagine yourself at a garage sale and you see some item — an antique, say — that strikes your fancy. You are tempted to purchase it. At that instant, you don’t know what the price is, but you DO know that there is a number for that price. In your mind, that number might have many different values: it could be $4 or $8 or $20. You don’t know the actual number, but you can imagine many different possibilities. If it’s only $4, you’ll snap it up. If it’s $40, you’ll laugh and walk away. But what if it’s $10? Or $15? Inside your head you are holding this abstract idea of “price”: a variable that could be many different values. 

Pounce on that idea! It is the second big idea behind programming: thinking about numbers in the abstract sense of a variable that could be almost any number. We have plenty of words for variables: speed, price, weight, height, width, length, distance, or temperature, for example.

In Javascript we have a simple way of creating a variable: we simply say “There will be a variable called x”:

var x;

But “x” is such a vague, arbitrary name for a variable. It’s always better to have a variable name that tells you what the number actually means, like these:

var speed;
var price;
var weight;

Here is the exact grammar you need to declare a variable: you start with the magic word “var”. This tells the computer that the next word is the name of a variable. Then you type a space. Then you type the name of the variable. Then you type a semicolon (;). Then you hit the RETURN key to go to the next line. 

You do this at the beginning of the program; this is called “declaring your variables” and serves as a nice way to establish who’s who and what’s what. It’s rather like introducing the attendees at a meeting at its outset.

Later on, when you are inside the program, you can assign numbers to variables with statements like this:

speed = 65;
price = 29.99;
weight = 173.9;