Skip to content
Prepare to Grow

Swift Fundamentals

Variables, Constants, and Data Types

Learn how Swift stores values, infers types, and uses constants to make intent clear.

Beginner4 min read2 guided activities

Before you begin

What you will understand

  • Choose between let and var.
  • Recognize common Swift value types.
  • Explain type safety and type inference.
  • Use explicit type annotations when they improve clarity.
01

Learning stage

Concept Explanation

A program works with values. Swift associates each stored value with a name and a type. Declare a constant with let when the value should not change, and declare a variable with var when reassignment is part of the design.

Swift is type-safe: a value must match the type expected at the place where it is used. The compiler can often infer a type from an initial value, but the type still exists even when it is not written explicitly.

  • Int represents whole numbers.
  • Double represents floating-point values.
  • Bool represents true or false.
  • String represents text and Character represents one extended grapheme cluster.
02

Learning stage

Syntax & Examples

The values below demonstrate constants, variables, inference, and an explicit annotation.

Profile.swiftswift
let displayName = "Maya"       // inferred as String
var completedLessons = 3     // inferred as Int
let progress: Double = 0.25  // explicit annotation
let notificationsEnabled = true

completedLessons += 1

print("\(displayName) completed \(completedLessons) lessons")
print("Progress: \(progress * 100)%")
Expected output includes “Maya completed 4 lessons” and “Progress: 25.0%”.
03

Learning stage

Real-World Usage

These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.

  • Use constants for configuration, identifiers, model values, and intermediate results that should remain stable.
  • Use variables for changing view state, counters, user input, and values that are intentionally updated during a workflow.
  • Type annotations are useful at API boundaries or when the intended type cannot be inferred from context.
04

Learning stage

Common Mistakes

Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.

  • Using var for every value even when reassignment never occurs.
  • Assuming an inferred numeric type will automatically convert to another numeric type.
  • Choosing vague names such as data or value that hide the purpose of a stored value.
  • Adding type annotations everywhere and making simple declarations harder to scan.
05

Learning stage

Practice Exercises

Work through these activities in a small Xcode project. No embedded playground is required on this site.

Activity 1

Classify stored values

For a profile screen, decide whether username, edit count, account creation date, and unsaved biography text should use let or var. Explain each choice.

Hint: Ask whether reassignment is part of the value's responsibility.

Activity 2

Predict the inferred types

Write declarations initialized with 42, 3.5, false, and "Swift". Predict each inferred type before checking it in Xcode.

Hint: Swift normally infers Int for integer literals and Double for floating-point literals.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

Which keyword declares a value that can't be reassigned after initialization?

  1. A.let
  2. B.var
  3. C.type
  4. D.value
Reveal answer

let

Use let whenever a stored value should remain stable after initialization.

QUESTION 2

Does omitting a type annotation make a Swift value untyped?

Reveal answer

No. Swift infers a concrete type from the expression and context.

Type inference reduces repeated syntax while preserving compile-time type checking.

07

Learning stage

Interview Preparation

Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.

What is the difference between let and var in Swift?

Short answer: let creates a constant binding and var creates a variable binding that can be reassigned.

Explore the detailed answer

Both declarations have a fixed type after Swift determines it. A let binding cannot be assigned a new value after initialization, while a var binding can receive compatible values later. Preferring let communicates intent and prevents unintended mutation.

Possible follow-ups

  • • What is type inference?
  • • Can a variable change its type later?
08

Learning stage

Key Takeaways

Names, types, and mutability form the base of every Swift program. Make stable values constants and let inference remove syntax only when intent remains clear.

  • Every Swift value has a type.
  • Use let by default and var for intentional reassignment.
  • Inference does not weaken type safety.
  • Numeric types are not implicitly interchangeable.