Lesson Notes and Walkthrough
By now you know how to create variables. This lesson answers the next key question: Should this value be changeable or fixed?
Core Rule
- Use
letwhen the value should stay the same. - Use
varwhen the value needs to change over time.
Simple Comparison
let appName = "BudgetBuddy" // fixed
var currentBalance = 120.0 // can change
currentBalance = 95.5How to Decide as a Beginner
- Ask: “Will this value ever change after setup?”
- If no, start with
let. - If yes, use
var.
Why Prefer let by Default
- It prevents accidental changes.
- It makes your code easier to reason about.
- It reduces bugs in larger projects.
Common Mistake
Beginners often declare everything with var. A better habit is to start with let, then switch to var only when the compiler tells you a reassignment is needed.