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

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 xtells you the type of a variable - โขReassigning a
constthrows a TypeError โ use it to protect important values - โข
letandconstare 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
- โข
constfor values that never change;letfor values that do - โขcamelCase naming:
playerScore,isLoggedIn - โขTemplate literals:
`Hello ${name}!`โ cleaner than string concatenation - โข
typeof xchecks the type at runtime - โขAvoid
varin modern JavaScript
Key Facts
- โJavaScript uses
+to join strings:"Hello " + name - โTemplate literals (backticks) are cleaner:
`Hello ${name}!` - โ
typeof xtells you the type of a variable - โReassigning a
constthrows a TypeError โ use it to protect important values - โ
letandconstare block-scoped โ they only exist inside the{}where they're defined
Real-World Examples
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
Which keyword is for values that don't change?