Swift Programming Tutorial: Variables, Constants, and Data Types
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: