Variables
6 min · Swift
Store values in named boxes called variables.
Enemy 1 of 5
A variable gives a value a name so you can use it again later without retyping it. In Swift, the most common way to create one is with let: let name = "Ada" creates a constant called name holding the text "Ada". From then on, writing name anywhere in your code refers to that stored value.
let creates a value that cannot be reassigned once set — Swift calls this a constant. If you try to assign a new value to a let later, the compiler will stop you with an error. When you genuinely need a value that changes over time (a score that goes up, a timer that counts down), Swift gives you var instead, which you'll start using in the next few lessons.
Apple's own style guidance, and most Swift code you'll encounter, favors let over var whenever possible — reach for let by default, and only switch to var when you have a concrete reason the value needs to change. This mirrors a pattern you'll see across modern languages (Rust's let vs let mut, Kotlin's val vs var): defaulting to immutable values make programs easier to reason about, because you never have to wonder whether something changed behind your back.
Loading editor...