Constants are just like variables except that their value cannot be altered. A constant contains a fixed value that cannot be altered by the program during its execution. Constants can be of any data type defined in the programming language.
Constants are declared using the let keyword. We can also provide a type annotation while declaring a constant to define the kind of values the constant can store.
import Swiftlet firstConstant = 53print(firstConstant)// Declaring constant by also providing type annotationlet anotherConstant:Float = 3.14159print(anotherConstant)
There is no possible way to alter the value of a constant once we have assigned it a value; doing so would result in an error. The following code shows that if we try to assign a new value to a constant, our program will output an error.
import Swiftlet firstConstant = 53print(firstConstant)// Modifying the value of the constantfirstConstant = 23print(firstConstant)