Let vs Var in Swift: Here’s the Real Difference

let and var being used in swift

In Swift, there are two types of variables:

  • Constants. These are immutable variables.
  • Variables. These are mutable variables.

To declare constants/variables in Swift:

  • let is used to declare an immutable constant. You cannot change its value of it later.
  • var is used to create a mutable variable that you can change later.

For example, to create a constant value in Swift, use the let keyword:

let num = 100.0

And to create a variable in Swift, use the var keyword:

var x = 200

Swift advises you to create constants whenever possible. You can see this in action when Xcode shows you warnings about using var.

Using constant values is meaningful when you want to declare variables that are not supposed to change.

When you make them constants using the let keyword, it is impossible to accidentally change them in the future.

Thanks for reading. Happy coding!

Scroll to Top