Let vs Var in Swift - codingem.com

What is the Difference between Let and Var in Swift?

In Swift, there are two types of values:

  • 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.

Further Reading

About the Author

Artturi Jalli
I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.

2 thoughts on “What is the Difference between Let and Var in Swift?”

  1. At the beginning, I was still puzzled. Since I read your article, I have been very impressed. It has provided a lot of innovative ideas for my thesis related to gate.io. Thank u. But I still have some doubts, can you help me? Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *