Skip to content
Learn Netverks

Lesson

Step 7/36 19% through track

variables-constants-swift

Variables and constants

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Variables and constants: the syntax, patterns, and safety habits you need before advancing in Swift.

Teams still ship Variables and constants in Swift codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Variables and constants in contexts like: iPhone/iPad/Mac apps, server-side Swift (niche), and Apple toolchain projects.

Write Swift in main.swift with print(), click Run on server—the dev runner swiftc compiles and runs the binary (requires Swift toolchain, typically macOS; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Swift is statically typed like Java and Kotlin. Use let for constants (immutable bindings) and var for variables that can change.

let vs var

let name = "Ada"      // constant binding
var score = 10        // mutable
score += 5

let does not make objects deeply immutable—only the variable cannot be reassigned. Prefer let by default for safer, clearer code.

Type annotations

let count: Int = 10
var price: Double = 9.99

Inference works when the initializer makes the type obvious. Explicit annotations help empty collections and public APIs.

Important interview questions and answers

  1. Q: let vs var?
    A: let assigns once; var allows reassignment—similar to val/var in Kotlin or const/let in JavaScript.
  2. Q: Does let make structs immutable?
    A: The binding is immutable; struct properties declared var can still mutate when the struct instance is var.

Self-check

  1. Which keyword declares a constant binding?
  2. Can you reassign a let variable?

Pitfall: let does not make struct properties immutable when the struct is var—only the binding is fixed.

Interview prep

let vs var?

let assigns once; var allows reassignment.

Does let deep-freeze objects?

No—only the variable binding is immutable; struct properties may still mutate when instance is var.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • let vs var?
  • Type inference?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump