Thursday, January 18, 2018

What is the difference between let and var in Swift?

The let keyword is used to declare constants while var is used for declaring variables.

let someConstant = 10
var someVariable : String


Here, we used the : string to explicitly declare that someVariable will hold a string. In practice, it's rarely necessary — especially if the variable is given an initial value — as Swift will infer the type for you. It is a compile-time error trying to use a variable declared as a constant through let and later modifying that variable.