HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to SwiftVariablesData TypesOperatorsStringsArraysConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Array & Dictionary
🔀 Condition & Loop
🧩 Function
Swift›Variables

Variables

Variables with var and constants with let; type annotation and value reassignment.

Simple enough for a 5-year-old to understand

var and let

var is a variable whose value can change, while let is an immutable constant. Its type is inferred from the value.

Example code
1
var name = "Alex"
Variable (can change later).
VariableVariable (can change later).
2
let birthYear = 1815
Constant (let = immutable).
VariableConstant (let = immutable).
3
name = "Alex L."
var is mutable.
Variablevar is mutable.
4
print(name)
Alex L. is the author.
Output / InputAlex L. is the author.
5
print(birthYear)
Author 1815.
Output / InputAuthor 1815.
VariableOutput / Input
💡 Hint: Use let for values that won't change; it is safer.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Which one is used for a constant that will not change?

About this topic

There are two ways to store values in Swift: let and var. A name defined with let receives a value once and never changes again; a name defined with var can take another value later. This distinction is one of Swift's most prominent habits.

The language's recommendation is clear: use let by default, but switch to var if you really need to change it. Xcode warns you when it sees a var you haven't changed and suggests using let. The reason for this is readability — anyone who sees let knows that value stays constant throughout the execution flow.

Swift usually infers the type itself. When you write let count = 5, its type becomes Int, even if you don't say so. If you want, you can explicitly write the type using a colon; this is especially mandatory for variables that will be assigned a value later.

What you will learn

  • The difference between let and var
  • How type inference works
  • How to explicitly specify the type
  • Naming conventions
  • Why using constants is the default

Who is this for?

  • Beginners to Swift
  • Those encountering the concept of immutability for the first time
  • Those coming from Python or JavaScript who want to get used to a type system

When to use?

Anything that doesn't change throughout the flow, such as a calculation result, a setting value, or data coming from the user, should be let; this provides information to both the compiler and the person reading the code.

Values that actually change, such as a counter, an accumulated total, or a state updated within a loop, should be var.

Common mistakes

Wrong

let total = 10
total = 20

A name defined with let is constant, a second assignment gives the error "cannot assign to value: 'total' is a 'let' constant". The compiler usually suggests a fix as well. If you are really going to change the value, you must make the definition var.

Correct

var total = 10
total = 20

Wrong

var score
score = 10

Swift infers the type from the initial value; if you don't provide a value, it cannot know the type and gives the error "type annotation missing in pattern". If you want to assign the value later, you need to explicitly write the type.

Correct

var score: Int
score = 10

Wrong

let price = 10
let total = price * 1.5

price became an Int through type inference, while 1.5 is a Double. Swift does not automatically convert two different number types to each other and gives the error "binary operator '*' cannot be applied to operands of type 'Int' and 'Double'". If you want the product to be decimal, you should choose the type as Double from the start.

Correct

let price: Double = 10
let total = price * 1.5

Frequently asked questions

Does using let everywhere affect performance?

It does not affect it negatively; on the contrary, since you are telling the compiler that the value will not change, it can perform certain optimizations. The main benefit of using let is not performance, but safety: it eliminates the possibility of accidental overwriting.

Can I use Turkish characters in variable names?

Technically Swift accepts Unicode names, and you can even use emojis. But using English and ASCII names in source code is generally accepted; it causes no issues in teamwork and library compatibility.

Should I always write the type?

No. If there is an initial value, type inference is sufficient and shortens the code. Writing the type makes sense when you will assign the value later, when you want a different type from the inferred one, or when it is helpful for readability.

Summary

let is constant, var is mutable.

The default choice should be let.

If there is no initial value, the type must be written explicitly.

Type inference does not automatically switch between Int and Double.

An unmodified var produces a compiler warning.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code