In JavaScript, you declare a variable using the let keyword, followed by the name, followed by = and the value:
let score = 0;let— the keyword that declares the variablescore— the name you use to refer to this variable0— the initial value assigned to it
There is no need to specify a type. JavaScript figures out the type from the value on the right.
let vs const#
Use let when the value may change later. Use const when the value should stay the same:
const maxRounds = 3; // will never change
let score = 0; // will change as the game progresses
Naming Rules#
- Names can contain letters, digits, underscores, and
$. - Names cannot start with a digit.
- Names are case-sensitive:
choiceandChoiceare different variables. - Use clear, descriptive names that reflect the value being stored.