juniorSwift

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:

  • var creates a variable, meaning its value can be changed later.
  • let creates 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.

Temperature.swift
var currentTemperature: Double = 21.5
currentTemperature = 23.0
let freezingPoint: Double = 0.0
// freezingPoint = -1.0 // Error: constants cannot be changed

The compiler can often infer a type automatically:

Swift
let language = "Swift" // Inferred as String
var score = 100 // Inferred as Int

However, developers can explicitly declare a type when clarity is needed:

Swift
let username: String = "Ava"
var attempts: Int = 3

Data 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:

TypeExamplePurpose
Int42Whole numbers
Double3.14Decimal numbers
String"Hello"Text
BooltrueTrue/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.

Swift
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

Rendering diagram…

Type safety and mutability

Two concepts interviewers often look for are:

  1. Mutability: Can the stored value change?
  • var allows reassignment.
  • let does not.
  1. 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:

CategoryExamplesBehavior
Value typesInt, String, Struct, ArrayA copy gets its own value
Reference typesClass, closuresMultiple 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.

CartItem.swift
let productName: String = "Wireless Keyboard"
var quantity: Int = 1
let price: Double = 49.99
quantity = 2

Here, 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?

More Swift interview questions

View all →