Swift Academy · Lesson

let vs var: Constants and Variables

Understanding immutability with let and mutability with var in Swift.

Lesson 1 of 412 steps

let vs var: Constants and Variables is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

In Swift, every value you work with is either a *constant* or a *variable*. Choosing correctly is one of the first habits that makes your code safer and easier to understand.

Declaring a Constant with let

Use `let` to declare a constant. Once assigned, its value never changes: ```swift let appName = "CoddyKit" let maxScore = 100 ``` The compiler enforces this — attempting to reassign will cause a compile error.

Declaring a Variable with var

Use `var` to declare a variable whose value can be updated: ```swift var score = 0 score = 42 score += 10 ``` Variables are ideal for state that evolves over time.

Why Prefer let?

Swift recommends using `let` by default and switching to `var` only when mutation is needed. This prevents accidental changes and helps the compiler optimise your code.

Naming Rules and Conventions

Swift identifiers use *camelCase* by convention: ```swift let playerName = "Alice" var currentLevel = 1 ``` Names are case-sensitive: `score` and `Score` are different identifiers.

Reassigning a let — Compiler Error

This code will not compile: ```swift let pi = 3.14159 pi = 3.0 // ❌ cannot assign to value: 'pi' is a 'let' constant ``` Swift catches this mistake at compile time, before your app runs.

Multiple Declarations on One Line

You can declare multiple constants or variables at once: ```swift let x = 5, y = 10, z = 15 var a = 1, b = 2 ``` Keep this concise; prefer separate lines for clarity when names or values are complex.

Deferred Initialisation

A `let` constant doesn't need an immediate value — you can assign it once later: ```swift let finalScore: Int if condition { finalScore = 100 } else { finalScore = 0 } ``` The compiler ensures it's assigned exactly once before use.

Printing Constants and Variables

Use `print()` and string interpolation to display values: ```swift let name = "Swift" var version = 5 print("Language: \(name), version: \(version)") ``` The `\(...)` syntax embeds any expression directly inside a string.

Practical Example: Game State

A real pattern: ```swift let playerName = "Alice" // won't change var lives = 3 // changes as game progresses var score = 0 lives -= 1 score += 250 ``` Using `let` where possible documents your intent for future readers.

Quick Check

Which keyword declares a value that **cannot** be changed after assignment?

Recap

Key takeaways: • `let` → constant, immutable after assignment • `var` → variable, can be reassigned • Prefer `let`; switch to `var` only when mutation is truly required • Both use camelCase naming and can be initialised later (let exactly once) Next up: how Swift infers types automatically.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “let vs var: Constants and Variables” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “let vs var: Constants and Variables”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “let vs var: Constants and Variables”?

Understanding immutability with let and mutability with var in Swift. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “let vs var: Constants and Variables” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. let vs var: Constants and Variables
  2. Type Inference and Explicit Types
  3. Numbers, Booleans and Characters
  4. String Fundamentals and Interpolation
← Back to Swift Academy