Swift Programming Tutorial: Variables, Constants, and Data Types

Arc Sosangyo
3 min readAug 5, 2021
Photo by: Alex Bachor at unsplash.com

This article is part of my Swift Programming Tutorial series.

What is a variable

Variables are used to store data. The stored data can be referenced or manipulated in a code. In other words, you can think of variables as containers that hold data.

How to declare variables in Swift

You can easily assign variables by declaring it using var as keyword.

E.g.

var firstVar = "Hello World"
print(firstVar)

The output will be:

Hello World

In above example, you declare firstVar as variable containing the value “Hello World”.

The variable’s value can be changed by assigning newer value.

E.g.

var firstVar = “Hello World” 
firstVar = “Hi Earth”
print(firstVar)

The output will be:

Hi Earth

You can also declare multiple variables by using comma:

var firstVar = “Hello World”, secondVar = “Hi Earth”, thirdVar = “Hey Planet”

Or you can format the syntax of the code like this:

--

--

Arc Sosangyo
Arc Sosangyo

Written by Arc Sosangyo

Arc is an iOS Dev and app publisher, a former IT manager who transitioned to iOS engineering, and a big fan of AI, coding, science, history, and philosophy.