Last updated

JavaScript Variables

Definition: A variable is a named container for a value. Modern JavaScript creates them with let (value can change) or const (value stays constant).

Example 1 — creating variables

let age = 25;
const name = "Talented";
console.log(name);
console.log(age);

Example 2 — let can change, const cannot

let score = 10;
score = 20;        // allowed with let
console.log(score);

const PI = 3.14;
// PI = 3;         // error: cannot reassign a const
console.log(PI);

Example 3 — updating a value

let count = 0;
count = count + 1;
count += 1;        // shortcut for the same thing
console.log(count); // 2

Naming rules

  • Start with a letter, $, or _ (not a number)
  • Case-sensitive; cannot be a reserved word like let

💡 Rule of thumb: use const by default; switch to let only when the value must change. Avoid the old var in new code.

Try it Yourself
Output

          
Ad · responsive