Swift Series #2

Swift Series #2

·

2 min read

Variable vs Constant

If we want to store some value in memory we need a storage area in memory. The storage area needs to be named in our code so that it can be accessed.

There are two ways we can store values in Swift. Either we can declare a variable or constant to store values.

Variable: Variables' value changes over time. We declare variables using the 'var' keyword.

var age = 20
age = 21

The above code means the age can be assigned a new value and it changes over time.

Constant: Constant do not change its value after it is assigned. We declare variables using the 'let' keyword. For example:

let dob = "02-12-2000"

It is not correct to change the value of date of birth after it is assigned. So, we have declared it with the let keyword, which in turn means that it is a constant

If you try to change the value of a constant, the compiler will give a compile-time error.

Keep Swifting....