Lesson 2 of 15

Variables & Constants

Variables and Constants

Swift uses let for constants and var for variables:

let pi = 3.14159     // constant — cannot be changed
var count = 0        // variable — can be changed
count = 10

Swift infers the type from the initial value. You can also annotate explicitly:

let name: String = "Alice"
var age: Int = 30

Basic Types

TypeExample
Int42
Double3.14
String"hello"
Booltrue, false

Your Task

Create a constant x with value 10, a variable y with value 5, then set y to y + x and print y.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.