let, and var are the swift keywords used to make the variables.

let is used to create immutable variables, whereas var is used to create a mutable variable.

The significant difference between let and var is that when you create an immutable variable called a constant using let, you need to assign a value before using it, which cannot be changed later. And when you create a mutable variable using var, you can set a value before using it or later, which means you can change the value at any time.

let

let a: Int = 10

print(a) // prints 10

a = 11 // Throws error "Cannot assign to value: 'a' is a 'let' constant"

print(a)

var

var a: Int = 10

print(a) // prints 10

a = 11  // No Error

print(a) // prints 11

0 Comments

Submit a Comment

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