Start learning free

LESSON 14 OF 415 · VARIABLES

let vs var in Swift: Constants and Variables

Lesson Read the full explanation

Every time you create a name in Swift, the language makes you answer one question first: will this value ever change? There are two keywords, one for each answer:

  • let declares a constant — the value is set once and can never be reassigned.
  • var declares a variable — the value is allowed to change later.

This is not just style. When you write let, the compiler guarantees the value never changes — if any line of your program tries to modify it, the program simply refuses to compile. Whole categories of bugs disappear before the code even runs. That is why Swift programmers reach for let by default and only use var when a value genuinely needs to change — a counter, a running total, a position that moves:

let pi = 3.14159      // fixed forever — perfect for let
var lives = 3         // will go down when the player is hit
lives -= 1            // OK — lives was declared with var
print(lives)          // prints 2

The trap:

Sooner or later you will try to change a constant. The compiler stops you with this exact error — read it carefully, because the note: underneath hands you the fix:

error: cannot assign to value: 'maxLives' is a 'let' constant
note: change 'let' to 'var' to make it mutable

The message names the exact constant (maxLives), points at the line that tried to change it, and the note tells you the two honest ways out: keep let and stop reassigning, or switch the declaration to var because the value really does need to change. Compound operators like += hit the same wall — left side of mutating operator isn't mutable: 'maxLives' is a 'let' constant.


Assignment
Follow the Coding Tutorial to build a tiny game setup with one constant and one variable:

  • Declare a constant maxLives equal to 3 — the rules of the game never change — and print it.
  • Declare a variable score starting at 0, add 10 and then 5 to it with +=, and print the total.

The exact expected output is:

maxLives = 3
score = 15

Hint
Use print("maxLives = \(maxLives)")-style string interpolation: \(...) drops a value into the middle of a string. And if the compiler says cannot assign to value or left side of mutating operator isn't mutable, the name you are changing was declared with let — the one value that must change here is score, so declare it with var.


STEP 1 OF 6
    Ctrl + Enter to check

    SWIFT · VARIABLES

    Let Vs Var in Swift, explained

    The question every Swift name answers

    When you create a name in Swift, you decide whether its value will ever change. Two keywords express the answer:

    • let declares a constant. The value is set once and can never be reassigned.
    • var declares a variable. The value can be reassigned later.
    let pi = 3.14159
    var lives = 3
    lives -= 1
    print(lives)
    
    2
    

    Pi never changes, so it is a let. The number of lives goes down when the player is hit, so it is a var. The update lives -= 1 works only because of the var.

    Why Swift cares

    With let, the compiler guarantees that no line of the program changes the value. Any attempt becomes a compile error, so a whole category of bugs cannot occur. This is why Swift code uses let by default and var only for values that really change, such as counters, running totals and positions.

    The habit works in the other direction too. A var that nothing ever changes only tells the reader that it might change, so it should be a let.

    The error you will meet

    Sooner or later you try to change a constant:

    let maxLives = 3
    maxLives = 5
    
    error: cannot assign to value: 'maxLives' is a 'let' constant
    note: change 'let' to 'var' to make it mutable
    

    The message names the constant and the note offers two ways out: stop reassigning it, or declare it with var because it really must change. Compound operators hit the same wall. For let maxLives = 3, the line maxLives += 1 reports left side of mutating operator isn't mutable: 'maxLives' is a 'let' constant.

    The exercise, step by step

    The program sets up a tiny game with one constant and one variable, and prints both. The expected output is exactly:

    maxLives = 3
    score = 15
    
    1. Declare the constant. let maxLives = 3. The rules of the game do not change, so let fits.
    2. Print it with interpolation. Inside a string, \(maxLives) is replaced by the value. Reading a constant is always allowed.
    3. Declare the variable. var score = 0. A score is a running total, so it has to be a var.
    4. Add 10 with score += 10. This line compiles only because score is a var.
    5. Add 5 with score += 5. Each update starts from whatever the variable holds at that moment, so the score is now 15.
    6. Print the total in the same label = value format.

    The solution in Swift

    let maxLives = 3
    print("maxLives = \(maxLives)")
    var score = 0
    score += 10
    score += 5
    print("score = \(score)")
    

    The details worth knowing:

    • The type is inferred. Both names are Int. A new value for score must also be an Int.
    • += is shorthand. score += 10 means score = score + 10.
    • Constants and collections. For an array, let means the contents cannot change either. With let list = [1, 2], calling list.append(3) is an error. The same array declared with var can grow.
    • A constant can be assigned once, later. let later: Int followed by later = 5 is allowed, as long as the assignment happens exactly once.

    Common mistakes

    • Declaring score with let. The first += fails to compile.
    • Declaring maxLives with var out of habit. It compiles, but nothing stops a later line from changing it.
    • Switching to var to silence an error without thinking. The error often points at a real design mistake.
    • Printing before the updates. The final print must come after both += lines to show 15.

    Key takeaways

    • Use let when the value is fixed and var when it has to change.
    • The compiler enforces the choice, so a mistaken change is caught before the program runs.
    • Start with let and switch to var only when needed.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free