Variables and Types
In Swift, you declare constants with let and variables with var. Constants can't be changed after they're set, while variables can be updated. This simple distinction helps you write safer code.
Swift uses type annotations to specify what kind of data a variable holds. For example:
let name: String = "Alice"
var age: Int = 25
let pi: Double = 3.14159
var isStudent: Bool = true
But here's the cool part—Swift can often figure out the type automatically. This is called type inference. So you can write let name = "Bob" and Swift knows it's a String.
Basic types include String for text, Int for whole numbers, Double for decimals, Bool for true/false values, and Character for single letters. Swift is type-safe, meaning it won't let you accidentally mix types—like adding a number to a string without converting it first.
Try it Yourself ->