Lesson 2 of 14 · 6 min

Variables

A variable is a name for a value, so you can use it again later. You create one with let or const:

const name = "Tariro";
let score = 10;
score = score + 5;
console.log(name);   // Tariro
console.log(score);  // 15
  • const is for values that never change. Trying to change a const is an error.
  • let is for values that will change, like a score or a counter.
  • score = score + 5 reads as "take the old score, add 5, and store the result back in score".

Values have types. "Tariro" is a string (text, in quotes). 10 is a number (no quotes). The quotes matter: "10" is text that happens to contain digits.

You can join strings and numbers with +:

console.log(name + " scored " + score);  // Tariro scored 15

Mind the spaces inside the quotes: + does not add any for you.

Your task

  1. Create a constant city with the value "Harare".
  2. Create a variable visitors that starts at 1200.
  3. On a new line, add 50 to visitors.
  4. Print Harare had 1250 visitors using your two variables.
main.js
// 1. Create the constant city


// 2. Create the variable visitors


// 3. Add 50 to visitors


// 4. Print the sentence

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…