Variables & Constants in Swift

From the Swift cheat sheet · Getting Started · verified Jul 2026

Variables & Constants

Declaration and initialization

swift
var mutableVariable = "Can change"
let immutableConstant = "Cannot change"

// Type annotations
var explicitString: String = "Hello"
let explicitInt: Int = 42

// Type inference
let inferredBool = true  // Bool
💡 Use `let` by default, only use `var` when you need mutability
⚡ Swift has strong type inference - explicit types often unnecessary
📌 Constants can be set once and only once, even after declaration
🟢 Computed properties calculate their value each time they are accessed

Continue with Swift

Save the full cheat sheet or work through every related task.

More Swift tasks

Back to the full Swift cheat sheet