var / let
Try it yourself →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
var score = 10 let brand = "Swift" score = 20 print(score) print(brand)
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.