both let and var used to declare variable then what is the difference between Let and Var.
when to use Let. plzz explain with a example the main usage of Let keyword. thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sundaram SubramanianRank #150 · 15.2K reputationPosted Jun 16, 2017, 1:25 AM EDT
The
letkeyword defines a constant:let theAnswer = 42The
theAnswercannot be changed afterwards. This is why anythingweakcan't be written usinglet. They need to change during runtime and you must be usingvarinstead.The
vardefines an ordinary variable.Ketan MokariyaRank #1,792 · 832 reputationPosted Jun 16, 2017, 1:51 AM EDT
Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 16, 2017, 4:44 AM EDT
let keyword in swift is used to declare constant value and immutable can never be changed once defined.
let name ="Kirit"
after you can change name = "Modi" then you got compiler error. So declare value of its only one time using let keyword.
Var :
var keyword is mutable in swift is used to declare variant value, That value can change at run time. it means can change it too many time.
var name = "Kirit"
You can changed name = "Modi" then successfully updated value of variable.
Gokhul VarmanRank #1,505 · 1.1K reputationPosted Aug 1, 2017, 6:19 AM EDT
Vivek KumarRank #272 · 9.3K reputationPosted Jun 16, 2017, 1:54 AM EDT