Declare a Variable in JavaScript

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 variable
  • score — the name you use to refer to this variable
  • 0 — 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: choice and Choice are different variables.
  • Use clear, descriptive names that reflect the value being stored.

Resources#