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.
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
42or0.2. - True or false (a boolean):
trueorfalse. Every comparison, like5 > 3, gives one of the two.
Run the example. Each console.log shows a value in the console, below the code.
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.
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:
constfor values that don’t change after they’re created, like the cafe’s name.letfor 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.
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.
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:
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.
What kind is it?
Where you’ll see this in the course
- In the 3D game,
let passed = 0counts how many rings you’ve flown through, andlet start = performance.now()keeps the time the race started. They’reletbecause 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 = 0remembers which color the background is now.
Mark it complete to track your progress.