🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
JavaScript

Variables

Variables store values. In JavaScript we use let or const. Use const when the value won't change, let when it might.

3 min 10 XP Lesson 2 of 21
Variables
🌐

Appy Says…

JavaScript has three ways to create variables: var (old, avoid it), let (use when the value changes), and const (use when it stays fixed). Knowing which to use makes your code cleaner and avoids subtle bugs.

📖

Variables in JavaScript

A variable stores a value under a name. JavaScript gives you three keywords for creating them — each with different rules about whether the value can change:

  • const name = "Alex"; — the value never changes (constant)
  • let score = 0; — the value can change (use this for most things)
  • var — old style, has quirky rules. Avoid in modern code.
  • Use camelCase for names: playerScore, highScore, isLoggedIn
🎮

const vs let — like a fixed vs adjustable setting

const is like your game's resolution setting — you set it once at the start and it never changes. let is like your volume slider — it changes every time you adjust it. Pick the right one based on whether the value needs to change.

🌍

Real-World Examples

  • const APP_NAME = "TikTok"; — the name never changes
  • let viewCount = 0; viewCount++; — the view count grows with every view
  • const userId = "abc123"; — once you log in, your ID is fixed
  • let currentTrack = playlist[0]; currentTrack = playlist[1]; — the current song changes
💡

Key Facts

  • JavaScript uses + to join strings: "Hello " + name
  • Template literals (backticks) are cleaner: `Hello ${name}!`
  • typeof x tells you the type of a variable
  • Reassigning a const throws a TypeError — use it to protect important values
  • let and const are block-scoped — they only exist inside the {} where they're defined
📌

Remember

Default to const. Only switch to let when you know the value will change. Never use var in new code. This is the rule used in every modern JavaScript project.

What You Learned

  • const for values that never change; let for values that do
  • camelCase naming: playerScore, isLoggedIn
  • Template literals: `Hello ${name}!` — cleaner than string concatenation
  • typeof x checks the type at runtime
  • Avoid var in modern JavaScript

Key Facts

  • JavaScript uses + to join strings: "Hello " + name
  • Template literals (backticks) are cleaner: `Hello ${name}!`
  • typeof x tells you the type of a variable
  • Reassigning a const throws a TypeError — use it to protect important values
  • let and const are block-scoped — they only exist inside the {} where they're defined

Real-World Examples

• <code>const APP_NAME = "TikTok";</code> — the name never changes • <code>let viewCount = 0; viewCount++;</code> — the view count grows with every view • <code>const userId = "abc123";</code> — once you log in, your ID is fixed • <code>let currentTrack = playlist[0]; currentTrack = playlist[1];</code> — the current song changes

Remember

Default to const. Only switch to let when you know the value will change. Never use var in new code. This is the rule used in every modern JavaScript project.

Quick Quiz

1 / 2

Which keyword is for values that don't change?