Bitpath

Lesson 2 · Unit 01

Variables

Names for values with let and const, three types, and text built from values

13 exercisesabout 3 min

What you practise

  • let and const, reassignment, numbers, strings and booleans, typeof, template literals, converting with Number() and String(), what + does to a string and a number

Try a question from this lesson

Pick one

The first line gives the value 4 a name. What do you think the second line prints?

JavaScript
let price = 4;
console.log(price * 3);

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Variables

A variable is a name for a value. let apples = 5; creates the name apples and stores 5 under it.

From then on you can write the name wherever you would write the value.

Here = does not mean "is equal to". It means: store the value on the right under the name on the left.

JavaScript
let apples = 5;
let pears = 2;
console.log(apples + pears); // 7

Concept

let or const?

const is for a name that keeps its value. let is for a name whose value will change.

Use const unless you know the value has to change. Then a reader can see at a glance what stays put.

Give a variable a name that says what it holds. Names made of several words are written likeThis.

JavaScript
const taxRate = 0.2; // stays the same
let itemCount = 0;   // will change
itemCount = itemCount + 1;
console.log(itemCount); // 1

Concept

Three types

Every value has a type.

A number is for arithmetic: 4, 3.5. A string is text in quotes: "hi". A boolean is one of two values, true or false, written without quotes.

The type decides what you can do with a value, and what + does to it.

JavaScript
console.log(typeof 3.5);  // number
console.log(typeof "hi"); // string
console.log(typeof true); // boolean

9 more exercises in the app

  • 3× type the answer
  • 4× multiple choice
  • 1× match the pairs
  • 1× fill the blanks

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy