Variable Declaration in Swift

Level 271 keystrokes

Shows the difference between var (a variable that can change) and let (a constant), updates count and prints "Swift: 15".

Swift code

// var declares a variable whose value can change; its type is inferred as Int
1var count = 10
// let declares a constant; assigning to name again would be a compile error
2let name = "Swift"
// Allowed because count is a var: 10 + 5 = 15
3count += 5
// \() inserts the values into the string: Swift: 15
4print("\(name): \(count)")

Output

Swift: 15
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Swift code and how often they appear.

  • "× 4
  • (× 3
  • )× 3
  • =× 3
  • \× 2
  • :× 1
  • +× 1

More Swift examples at level 2

← Swift typing practice