๐Ÿ‡ฌ๐Ÿ‡ง 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?

    Variables โ€” Applaa Academy