lecture 1 of 150 completed

Values and variables

A test keeps a value it read so it can compare it later. const and let decide whether that value can change.

What you'll learn

  • What a value and a variable are
  • How let and const differ
  • Why a test stores the first reading under its own name

A test checks that borrowing a book lowers the number of copies by one. It reads the count, borrows the book and reads the count again. Its report shows the same number twice.

The examples in this track use Shelf, an invented library site. It is not a TestAcademy practice app. Here is the part of the test that keeps the numbers:

let copies = 4;
let before = copies;
copies = copies - 1;
before = copies;
console.log("before: " + before);
console.log("after: " + copies);
before: 3
after: 3

Let's read it one line at a time.

  1. 4 is a value. A value is one piece of data, such as a number, some text, or true and false. copies is a variable, which is a name for a value. let creates the name, and = gives it a value. Read = as "now holds", not as "equals".
  2. before gets the value that copies holds right now, which is 4.
  3. copies now holds 3. before still holds 4, because it received the number and not a link to copies.
  4. This line is the bug. It gives before a new value, so the first reading is lost.
  5. console.log prints what you put inside its brackets. before now holds 3, so this prints 3.
  6. copies holds 3 as well, so the two lines of the report show the same number.

Nothing warned us on line 4. let allows a name to take a new value at any time.

Protect the first reading with const

const also creates a name. The difference is that a const name can never be given another value.

let copies = 4;
const before = copies;
copies = copies - 1;
console.log("before: " + before);
console.log("after: " + copies);
before: 4
after: 3

before keeps the first reading. copies is still a let, because it has to change.

If someone later adds the bad line again, the code stops:

const before = 4;
before = 3;
TypeError: Assignment to constant variable.

The language guarantees a TypeError here. A TypeError is the error JavaScript raises when a value is used in a way its kind does not allow. The words after it come from the engine. Chrome and Node print this sentence. Firefox and Safari word it differently.

The three kinds of value you meet first

  • A number, such as 4 or 0.5.
  • A string, which is text inside quotes, such as "Dune".
  • A boolean, which is true or false.

A variable can hold any of them. The name does not record which kind it holds, so copies could later hold "four" and nothing would stop it. The next lecture shows how that causes a wrong total.

In a code review, expect this comment on a let that never changes: "use const". It is more than style. A reader who sees const knows the value on that line is the value everywhere below it.

Remember this

Give every value you want to compare later its own name. Use const unless the value must change. Use let only for the value that changes.

Reference: Declarations in the MDN JavaScript Guide.

now prove it

Reading is the setup. Solve this exercise in the editor. We run your code on each case and compare what it prints with the expected output.