Member-only story

let or var in Swift, and why it absolutely matters

The compiler may complain all the time if you get it wrong!

Steven Curtis
2 min readDec 11, 2019

When using Swift, you are allowed to create both variables and constants. The two are different things, but what are they and why does Swift think it so important to make the distinction?

Photo by Oladimeji Odunsi on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Be able to produce a “Hello, World!” Swift application (guide HERE)

OR

  • Be able to use Swift Playgrounds (guide HERE)

Terminology

compiler: A program that converts instructions into a machine-code or lower-level form so that they can be read and executed by a computer.

constant: A value that can not be amended during the normal execution of a program

variable: A variable that can be amended during the normal execution of a program

Making a variable

We can have a constant anywhere in our code

var width = 5

width = 6

So after the second command, width will store the value of 6. Great!

Making a constant

--

--

No responses yet