Variables and Constants in Swift: `var` vs `let` Explained


Every program needs to store information — a character’s name, a movie’s release year, whether a toy is still in the box. In Swift, you store values using variables and constants. Knowing which one to use is one of the first decisions you’ll make as a Swift developer.

In this guide, you’ll learn the difference between var and let, why Swift prefers constants, and how to name your values following Swift conventions.

What You’ll Learn

What Are Variables and Constants?

A variable is a named container that holds a value you can change later. A constant is a named container whose value is set once and can never change.

Think of it like labels on toy boxes in Andy’s room. A variable is a box labeled “Favorite Toy” — Andy can swap Woody for Buzz whenever he wants. A constant is a box labeled “Birthday Gift 2005” — once the gift is in the box, it stays there forever.

Creating Variables with var

Use the var keyword to create a variable. You can change its value as many times as you like:

var favoriteMovie = "Toy Story"
print(favoriteMovie)

favoriteMovie = "Finding Nemo"
print(favoriteMovie)
Toy Story
Finding Nemo

The value of favoriteMovie started as “Toy Story” and then changed to “Finding Nemo”. Variables are flexible — they let your data evolve as your program runs.

Creating Constants with let

Use the let keyword to create a constant. Once you set its value, it cannot be changed:

let releaseYear = 1995
print(releaseYear)
1995

If you try to change a constant, Swift won’t let you:

let releaseYear = 1995
// releaseYear = 2000  // ❌ Error: cannot assign to 'let'

The compiler catches this mistake before your code ever runs. This is one of Swift’s safety features — it prevents accidental changes to values that should stay fixed.

When to Use var vs let

The rule is simple: use let by default. Only switch to var when you actually need to change the value later.

Why? Constants make your code safer and easier to understand. When you see let, you know the value will never change — no need to trace through your code to figure out where it might get modified.

let movieTitle = "Up"         // Won't change — use let
var watchCount = 0            // Will change — use var
watchCount += 1
print("\(movieTitle): watched \(watchCount) time(s)")
Up: watched 1 time(s)

Tip: If you create a variable with var but never change it, Xcode will show a warning suggesting you switch to let. Listen to the compiler — it’s helping you write better code.

Naming Conventions

Swift uses camelCase for variable and constant names. The first word is lowercase, and each following word starts with a capital letter:

let movieTitle = "Monsters, Inc."
var currentScore = 8.5
let isAnimated = true
var numberOfSequels = 2

A few rules to follow:

  • Be descriptive. movieTitle is better than mt or x.
  • Start with a lowercase letter. Types like String start uppercase, but values start lowercase.
  • No spaces or special characters in names (underscores are allowed but not preferred in Swift).

Note: Swift supports Unicode in names, so let café = "Gusteau's" is technically valid. But stick to English letters and camelCase for readable, maintainable code.

Common Mistakes

Trying to Change a Constant

// ❌ Don't do this
let director = "Pete Docter"
// director = "Brad Bird"  // Error!
// ✅ Use var if you need to change the value
var director = "Pete Docter"
director = "Brad Bird"
print(director)
Brad Bird

If you need to change a value later, declare it with var from the start.

Using Vague Names

// ❌ Hard to understand
var x = "Woody"
var n = 3
// ✅ Descriptive names tell the story
var characterName = "Woody"
var sequelCount = 3

Good names make your code read almost like a sentence. Someone looking at sequelCount immediately knows what the value represents.

What’s Next?

  • Use var to create values that can change
  • Use let to create values that stay fixed
  • Default to let — only use var when mutation is needed
  • Follow camelCase naming conventions
  • Choose descriptive names that explain what the value holds

Now that you can store values, you need to understand what kinds of values Swift supports. Head over to Swift Data Types to learn about strings, integers, doubles, and booleans.