Constants and Variables

Constants are named values that cannot be changed. They are declared using the let keyword. Variables are named values that can be changed. They are declared using the var keyword.

let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 2

You can declare multiple constants or multiple variables on a single line, separated by commas:

let x = 0.0, y = 0.0, z = 0.0
var a = 0.0, b = 0.0, c = 0.0

Type Annotations

You can specify the type of a constant or variable by using a colon and the type name. For example, let pi: Double = 3.14 specifies that pi is a constant of type Double.

let pi: Double = 3.14
var x: Int = 0
let welcomeMessage: String = "Hello"

You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name:

let red, green, blue: Double