Variables & Data Types
Swift has two ways to declare a value: let for a constant that can never be reassigned, and var for a variable that can. Reaching for let by default, and only using var when you genuinely need to reassign, is idiomatic Swift.
let vs var
Swift main.swift
let name = "Priya" var score = 10 score += 5 print(name, score)
Output
Priya 15
Trying to reassign name after its first assignment is a compile error, not a warning — Swift enforces immutability by default rather than trusting you to respect it.
Type inference and explicit types
Swift infers a value's type from what you assign it, but you can also state the type explicitly with a colon:
Swift main.swift
let age: Int = 30 let price: Double = 19.99 let isAvailable: Bool = true let label: String = "In stock" print(age, price, isAvailable, label)
Output
30 19.99 true In stock
Type safety
Swift main.swift
var count = 5 count = "five" // compile error
Compiler output
error: cannot assign value of type 'String' to type 'Int'
Note: once Swift infers
count is an Int from its first assignment, that type is locked in for the rest of the variable's life — there's no implicit conversion between types like Int and String, even for something as reasonable-looking as reassigning a number-holding variable to a different type. You'd need an explicit conversion like String(count) to go the other way intentionally.