Swift is statically typed like Java and Kotlin. Use let for constants (immutable bindings) and var for variables that can change.
let vs var
let name = "Ada" // constant binding
var score = 10 // mutable
score += 5
let does not make objects deeply immutable—only the variable cannot be reassigned. Prefer let by default for safer, clearer code.
Type annotations
let count: Int = 10
var price: Double = 9.99
Inference works when the initializer makes the type obvious. Explicit annotations help empty collections and public APIs.
Important interview questions and answers
- Q: let vs var?
A:letassigns once;varallows reassignment—similar toval/varin Kotlin orconst/letin JavaScript. - Q: Does let make structs immutable?
A: The binding is immutable; struct properties declaredvarcan still mutate when the struct instance isvar.
Self-check
- Which keyword declares a constant binding?
- Can you reassign a
letvariable?
Pitfall: let does not make struct properties immutable when the struct is var—only the binding is fixed.
Interview prep
- let vs var?
let assigns once; var allows reassignment.
- Does let deep-freeze objects?
No—only the variable binding is immutable; struct properties may still mutate when instance is var.