From Zero to Deploy · Module 2 · Lesson 2.1

2.1 Values and variables, the labeled boxes

The kinds of value that show up in every app, how to keep a value under a name, the difference between const and let, and how to build text.

⏱ 15 min

Every program works with values

Everything an app keeps and shows is a value: the name of a cafe, a distance, whether it’s open or not. The three kinds you’ll see most:

  • Text (the AI calls it a string): always in quotes, like 'Café da Praça'.
  • A number: no quotes, like 42 or 0.2.
  • True or false (a boolean): true or false. Every comparison, like 5 > 3, gives one of the two.

Run the example. Each console.log shows a value in the console, below the code.

Try it

Three kinds of value

Console
Nothing in the console yet. Use console.log(...) to show values here.

Challenge: add the lines console.log('2' + '2') and console.log(2 + 2). The results are different because the first pair is text: + glues one text onto the other, so '2' + '2' becomes '22'. This detail causes real bugs, for example when a number typed into a form arrives as text.

A variable is a labeled box

A variable keeps a value under a name, so you can use it later. Think of a box with a label: the label is the name, and what’s inside is the value.

Try it

Your first variables

Console
Nothing in the console yet. Use console.log(...) to show values here.

Challenge: create a variable rating with the value 4.5 and show it in the console with console.log('Rating:', rating).

const or let?

You may have noticed the example uses two words to create variables:

  • const for values that don’t change after they’re created, like the cafe’s name.
  • let for values that will change, like the number of visits.

The AI uses const almost always and let only when it has to. That helps when reading code: when you see let, you know that value changes somewhere.

Find the bug

The console shows the error “Assignment to constant variable” on line 3. Which line needs to change?

Click the line with the problem.

Now it’s your turn. Write the code in the editor and click Run tests: the tests check each part and tell you what’s missing.

Exercise

Your visits to the cafe

Follow the steps in the comments: create a variable called name (text), one called km (a number), and one called visits, starting at 0. Then add 1 to visits twice and show all three variables in the console.

JavaScript
Tests
Click Run tests to check your code.
Console
Nothing in the console yet. Use console.log(...) to show values here.

Building text

Almost every app puts values into a piece of text to show on screen. There are two ways, and you’ll see both:

Try it

Putting text and values together

Console
Nothing in the console yet. Use console.log(...) to show values here.

The second way uses backticks (`, usually the key left of the 1) and puts each value inside ${ }. It’s easier to read, and the AI uses it almost every time.

Challenge: on the backtick line, add , which is ${minutes * 80} meters away right before the closing backtick. Whatever is inside ${ } can be a calculation.

Exercise

The invite

Build the sentence from the comment inside invite, using backticks and the three variables with ${ }. Don’t type “Ana” or “6”: if the variables change, the sentence should change with them.

JavaScript
Tests
Click Run tests to check your code.
Console
Nothing in the console yet. Use console.log(...) to show values here.

What kind is it?

Sort it

Text, number, or true/false?

  • 'Ana'
  • 42
  • '42'
  • true
  • 'true'
  • 0.5
  • 5 > 3

Where you’ll see this in the course

  • In the 3D game, let passed = 0 counts how many rings you’ve flown through, and let start = performance.now() keeps the time the race started. They’re let because they change during the game.
  • In the map app, let you = [-18.9186, -48.2772] keeps your position (latitude and longitude). It changes when the browser finds out where you are.
  • In the AI app, the server keeps the kinds of photo it accepts in const ACCEPTED_TYPES. That list never changes while the app runs.
  • In the “Hello, world!” button, let index = 0 remembers which color the background is now.
Task

Practice

0 of 5 done

Checkpoint

Check what you learned

  1. You're going to keep how many rings the player has flown through. const or let?

  2. What does console.log('2' + '2') show?

  3. What does this code show?

    const cafe = 'Torra Fina';
    console.log(`I went to ${cafe}`);
  4. Which of these is a true/false value (a boolean)?

Finished the lesson?

Mark it complete to track your progress.