What are variables, constants, and data types in Swift?
Updated Feb 20, 2026
Short answer
In Swift, variables are named storage locations whose values can change, while constants are fixed after initialization. Data types define what kind of value a variable or constant can hold, such as Int, String, or Bool. Swift uses strong type safety to catch many mistakes before code runs.
Deep explanation
Variables and constants
Swift provides two ways to store values:
varcreates a variable, meaning its value can be changed later.letcreates a constant, meaning its value cannot be reassigned after it is set.
Prefer `let` by default and use `var` only when a value must change. This makes code easier to understand and reduces accidental modifications.
var currentTemperature: Double = 21.5currentTemperature = 23.0
let freezingPoint: Double = 0.0// freezingPoint = -1.0 // Error: constants cannot be changedThe compiler can often infer a type automatically:
let language = "Swift" // Inferred as Stringvar score = 100 // Inferred as IntHowever, developers can explicitly declare a type when clarity is needed:
let username: String = "Ava"var attempts: Int = 3Data types in Swift
A data type tells Swift what kind of data a value represents and what operations are allowed on it.
Common Swift data types include:
| Type | Example | Purpose |
|---|---|---|
Int | 42 | Whole numbers |
Double | 3.14 | Decimal numbers |
String | "Hello" | Text |
Bool | true | True/false values |
Array | [1, 2, 3] | Ordered collections |
Dictionary | ["id": 1] | Key-value collections |
Swift is a strongly typed language. This means a value of one type cannot usually be used where another type is expected without conversion.
let age: Int = 25// let message: String = age // Error
let message = String(age)The compiler uses types to prevent bugs, such as accidentally adding text to a number or passing the wrong kind of value into a function.
How values flow through Swift code
Type safety and mutability
Two concepts interviewers often look for are:
- Mutability: Can the stored value change?
varallows reassignment.letdoes not.
- Type safety: Is the value used with the correct type?
- Swift checks many type errors during compilation.
- Explicit conversions are required between incompatible types.
`let` does not make an object completely immutable in every case. For example, a let constant containing a class instance cannot point to a different instance, but the object's internal properties may still change if they are declared as mutable.
A useful interview rule: let controls whether a reference or value can be reassigned, while the data type controls what that value can represent.Value types and reference types
Swift has both value and reference semantics:
| Category | Examples | Behavior |
|---|---|---|
| Value types | Int, String, Struct, Array | A copy gets its own value |
| Reference types | Class, closures | Multiple references can point to the same object |
For a junior interview, the key point is that most common Swift data types are designed around safety and predictable behavior.
Swift's type system helps developers find mistakes before users encounter them.
Real-world example
Imagine building a shopping app. A product's name should not change after loading, but a user's cart quantity should update as they shop.
let productName: String = "Wireless Keyboard"var quantity: Int = 1let price: Double = 49.99
quantity = 2Here, productName and price are constants because they describe fixed product information. quantity is a variable because the customer can add more items.
Common mistakes
- * **Using `var` everywhere** - This makes code harder to reason about
- use `let` unless a value must change.
- * **Ignoring types** - Assuming Swift will convert values automatically causes compiler errors
- convert values explicitly.
- * **Confusing constants with immutable objects** - A `let` reference may still point to an object whose properties can change.
- * **Using the wrong numeric type** - Mixing `Int` and `Double` directly fails
- choose the correct type or perform a conversion.
Follow-up questions
- What is the difference between let and var in Swift?
- What are optional data types in Swift?
- Why is Swift called a strongly typed language?
- What is type inference in Swift?