var / let
Variables (var) and constants (let).
Syntax
var name = value // let constant = value
Description
Values defined with var can change; values defined with let are constant (cannot be reassigned). The type is inferred from the value, or you can explicitly write it using :.
Example
Loading editor…
Expected output
20 Swift
When to use?
Use let for values that won't change (safer).
Common mistakes
- ⚠️ Reassigning to a let constant causes a compilation error.