Variables: let and const

Store values with let and const, change them, and see what a variable holds before it is given a value.

JavaScript · beginner · 5 min · 7 steps · free

What you'll learn

Variables.js

let score = 10;
const player = 'Ada';
score = score + 5;
let bonus;
console.log(player, score, bonus);
bonus = score * 2;
console.log(`${player} has ${bonus} bonus points`);

Step by step

  1. let score = 10 creates a variable named score and puts 10 in it.
  2. player is a const: it will always hold "Ada".
  3. The right side is worked out first, 10 + 5, then the result goes back into score: 15.
  4. let bonus; creates a variable without a value. JavaScript gives it the special value undefined.
  5. It prints Ada 15 undefined.
  6. bonus changes from undefined to 30.
  7. A template literal, between backticks, puts values straight into text with ${…}.

Common mistakes

Best practices

Key takeaways

Check yourself

const x = 1; x = 2; gives?

Answer: A TypeError. const cannot be reassigned.

let y; what is y?

Answer: undefined

More in The Basics