1
votes

I have just started learning new Swift Language, How can we differentiate like when to use "Let" and "Var". As i have understanding of Java Script i know while you want to define "Var". How "Let" is different than "Var".

Any help would be appreciated.

1

1 Answers

16
votes

First off... Swift != Javascript

The main difference is that var declares a variable and let declares a constant.

Like this...

var a = 1
// a is instantiated as 1

a = 2
// a is now 2

but this...

let a = 1
// a is instantiated as 1

a = 2
// this will return a compile time error

There are other smaller things too.