Variables and Constants in Swift: A Comprehensive Guide

In Swift, a variable represents a value that can be changed later on. The idea of a variable in programming is the same as variables in math.

To create a variable in Swift, use the var keyword.

For instance, let’s create an integer variable that represents the number 10:

var x = 10

A constant is a value that cannot be changed later on. To create a constant, use the let keyword.

For example, let’s create a constant that represents the name “Alice”:

let myName = "Alice"

In this article, you start learning Swift from the basic building blocks, that is, from variables and constants.

Let’s jump into it!

Variables and Constants in Swift

Any program or app needs to store data.

This data could be the user’s favorite color, contents of a shopping cart, username, or anything similar.

In Swift, there are two ways to store values in your app:

  1. Using variables.
  2. Using constants.

A variable is a value that can be changed later.

A constant is a value that cannot be changed later on.

Why Do We Need Both Variables and Constants?

At this point, you might wonder why we need both variables and constants.

Wouldn’t it be easier to only have variables? A constant could then be a variable whose value you never change.

The reason for having both is simple: Programmers make mistakes.

You may accidentally end up changing a value that was not meant to change. This could totally mess up your app.

Now that you understand what are variables and constants, it is time to learn how to create them. At the same time, this is going to be the first Swift program you ever write!

How to Create a Variable in Swift

In Swift, you can create a variable using the var keyword.

Now, open up an Xcode Playground file, such as the one you created in the previous section. Then place the following piece of code into the code editor:

var x = 10

It is that simple!

This is a simple Swift program that has a variable x that holds a value of 10.

Notice that the variable name can be longer than one character.

As another example, let’s create another variable called myAge:

var myAge = 26

Feel free to add this to your Playground file as well.

Now you understand how to create variables in your Swift program.

Next, let’s take a look at how to change the value of a variable.

How to Change a Variable in Swift

As you learned in the previous chapter, you need to use the var keyword to create a new variable in your Swift program.

To change the value of an existing variable, just assign a new value to it.

For example, here we create a variable, give it a value of 10 and then change it to 20:

var x = 10
x = 20

Notice how you do not use the var keyword when changing the variable. The var keyword is only used when creating a variable. When you access an existing variable, you should not use the var keyword. Instead, you can use the name of the variable alone.

Amazing! Now you understand what is a variable, how to create one, and how to update it.

It is time to familiarize yourself with constants. Let’s start by learning how to create a constant.

How to Create a Constant in Swift

To create a constant in Swift, use the let keyword.

For example, let’s create a constant that represents a color of a house:

let houseColor = "Yellow"

Now houseColor is a constant that cannot be changed.

If you try to change a constant, you are going to see a red error bar, that stops you from running the program.

Naming Conventions

Giving a name to a constant or variable seems easy.

However, as it turns out, it tends to be quite difficult.

This is because the name should be descriptive and short at the same time.

Whenever you name a variable, try to be as clear as possible.

Here are the basic rules for giving names:

  1. Avoid abbreviated names. Even if the names are long, spell them out.
  2. If you have a name that consists of multiple words, use the camel case. In other words, start consecutive words with capital letters, such as myBigHouse.
  3. Also, do not use spaces or special characters in variable names. Some of those cause errors, while some just look bad!

Here are some examples of well-named variables and constants:

var distanceToHouse = 100.0
let myFavoriteCar = "Lamborghini"
let targetNumber = 6

And here are some bad examples:

var d-to-House = 100.0
var myfavcar = "Lamborghini"
let tarNum = 65

These latter ones are hard if not impossible to understand.

So please, follow good naming conventions right off the get-go.


Today you learned how to store data values in your app using variables and constants.

To recap:

  • A variable can be created using the var keyword. A variable is a value that can be changed later on.
  • A constant can be created using the let keyword. A constant cannot be changed later on.

Storing values is one of the basic building blocks of programming. You’re going to need them a lot!

Thanks for reading. Happy coding!

Scroll to Top