Skip to content

Variables

Computer programs mostly deal with data, electronic data. In this section we're going to look at what data are and how we store and manipulate data.

Computer programs do three basic things from our perspective.

  1. They get data into the program
  2. They process the data
  3. They output the data

What's Data

Let's first look inside the computer at RAM. RAM stands for R**andom **A**ccess **M**emory. This means that the computer can get something in **RAM from anywhere very quickly. RAM is where the computer stores its data, including the operating system and the programs themselves. How do we imagine RAM? Here's one favorite way:

tapeMeasure

RAM is one long linear chain of bits. And a bit is a very simple thing, it's just a 0 or a 1. Think of each mark on the tape measure as a single bit. Data is just a collection of bits in a known spot. Say, starting at 5" on the tape measure and going for 10 marks.


What's a Variable

Computer programs need a way to find and use the data that they need. Data within a program are stored in variables. Physically as it were, a variable is a spot in RAM that has been given a name.

But from a software perspective, you can think of variables as containers.  This container doesn't hold water, or some other physical world substance--it holds data.  Technically in JavaScript, it holds references to data.  And since in the JavaScript language, when you set up variables, you don't specify what kind of data are going to eventually be placed into the variables, you can think of JavaScript variables as being kind of "general purpose" containers.

cupMedium

Here's how we do that in JavaScript:

1
2
3
    // Variables in JavaScript

    let count;
  • We start with the word let. This tells JavaScript that it is to create a variable. The word let is required by JavaScript in order to correctly make a variable.

  • Then we have the name of the variable, in this case, count.

  • And, finally, we have a semicolon ;, which is required by course standards.

  • However, at this point, the variable doesn't have any value in it. Here's the variable declaration over again with a small addition.

1
2
3
4
5
    // Variable assignment

    let count;

    count = 1;
  • We've added the count = 1 (variable name, a space, an assignment operator, a space, and the number 1).

  • This line puts the value 1 into the variable count.  (Technically, it puts a reference to the number 1 into it.)

  • JavaScript allows you to declare and assign variables on the same line. Normally, variables are not assigned starting values on the same line that creates them, for this reason this is not recommended. Unless the variables are constants, which we will discus in a later unit.

1
2
3
    // A variable with an initial value provided at the time of declaration

    let count = 1;

Read more

Related Reading

Pages 15 - 21 — Chapter 2.

Remember the coding standards in class may differ from the book.

Hands on work

Demo

  1. Demo: Variables in JavaScript unit01/demos/demo-variables.html
Here's the final frame from the animated demo:

VariableTableFinal