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:
scoreandScoreare 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
- Create a constant called
citythat holds the name of a city as a string. - Create a variable called
visitorswithlet, starting at100. - On a later line, change
visitorsto250. - Print both with one
console.log, the city first.
- There is a constant
citythat holds a string visitorsis created withletand starts at100visitorsis changed to250afterwards- One
console.logprints the city and then250
Lesson complete!
Next Lesson: Numbers and Math