JavaScript Basics · Lesson 2 of 13

Variables

A variable is a name for a value. Once a value has a name, you can use it again and again.

let score = 10;
console.log(score);

let creates the variable, score is its name, and = puts the value 10 into it. Read = as "becomes", not "equals".

A variable made with let can be given a new value later. You do not write let again:

let score = 10;
score = 15;
console.log(score);   // 15

const

If a value should never change, create it with const instead. Trying to change it later is an error, which protects you from accidents:

const daysInWeek = 7;

Many programmers use const by default, and let only for values that need to change.

Names

  • Names can contain letters, digits, _ and $, and cannot start with a digit.
  • Names are case sensitive: score and Score are different variables.
  • When a name is made of several words, JavaScript programmers write it in camelCase: highScore, firstName.

Choose names that say what the value means. price is better than p.

Printing several things

console.log accepts several values separated by commas, and prints them with spaces between:

console.log("Score:", score);

Your Task

  1. Create a constant called city that holds the name of a city as a string.
  2. Create a variable called visitors with let, starting at 100.
  3. On a later line, change visitors to 250.
  4. Print both with one console.log, the city first.
  • There is a constant city that holds a string
  • visitors is created with let and starts at 100
  • visitors is changed to 250 afterwards
  • One console.log prints the city and then 250