What is the Difference between Let and Var in Swift?

It’s important you understand the difference between let and var in Swift.

  • let is used to declare a constant that you cannot change later.
  • var is used to create a variable that you may change later.

For example, to create a constant, use let:

let num = 100.0

And to create a variable, use var like this:

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. This is helpful because it prevents you from accidentally changing values that are not meant to be changed.

So use var‘s only when you need to change the value in the future.

Further Reading

Swift – Upgrade Your Skills with These 5 Tips

iOS Development Tips in 2021

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

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