We’ve manipulated variables in the last few exercises, but what about variables with values we don’t want to change? That’s the job for constants.

Whereas variables can be reassigned during their lifetime, constants cannot. Once we assign a value to a constant in Swift, it becomes immutable. This means that the value stored in it cannot be changed.

In Swift, constants are declared using the let keyword:

let pi = 3.14

So why do we use constants? Primarily, constants are used as a safety measure. By using constants, we are making our programs safer by guaranteeing that we only change the values we expect to and thus prevents some potential bugs.



Declare a constant named months and assign it the number of months that end with “y”.

E.g. January ends in “y”.

Folder Icon

Take this course for free

Already have an account?