Learn
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.
Instructions
1.
Declare a constant named months
and assign it the number of months that end with “y”.
E.g. January ends in “y”.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.