Constants in Swift

We just saw how to declare a variable in swift using the “var” keyword. Now we will learn how to declare constants in swift.

To declare constants in swift we make use of the “let” keyword. Constant is a set of value/values which cannot be changed at any time in the program. The constants lets you finalize a value. The following is the syntax for Declaring a variable as a constant.

constant-let eg

if we try to re-use the variable noOfMonths and re-assign a new value; we get an error as shown below.

constant error

Constants have similar property as Variables in Swift. Swift also Performs ‘Type inference’ implicitly. It can also perform ‘Type annotation’.

constants 3

 

next we will learn about emoji variables.