What is the difference between let and var in Swift?
What is the difference between let and var in Swift?
In Swift, the keywords let
and var
are used to declare constants and variables, respectively. Here are the key differences between them:
let
(Constants)let
are immutable, meaning they cannot be changed once they are set. This ensures that the value remains constant throughout the program.let
keyword.let studentName = "Arpit Mishra"
In this example, studentName
is a constant and its value cannot be changed after its initial assignment.var
(Variables)var
are mutable, meaning they can be changed or reassigned after their initial declaration.var
keyword.var grade = 7
In this example, grade
is a variable and its value can be changed later in the program.let |
var |
---|---|
Called constants | Called variables |
Immutable | Mutable |
Declared with let keyword |
Declared wi... |
middle
Chưa có bình luận nào