Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz


Variable Declaration in Swift and Type Annotations

2 min 12 sec read

What are Variables?

Like human memory, Computer memory has millions of cells where it can store data or information. Now to retrieve information from those cells, each cell should be given one name called " variable name". These variables are most useful and can be of different types like integer, float, double, char, strings.

Variable Declaration in Swift :

To declare variables in Swift "var" keyword is used. Semicolons are not compulsory in Swift Language. But remember if you add two statements in a single line you have to use ";" as the delimiter to separate them. Otherwise, it shows syntax error.

Syntax :-

var variablename = Constant Value

Example :-

var name = "Manish"

Let's create a sample playground , Goto File > New > Playground > Next.




import UIKit
var a = 20

Run the program in the playground.



You can declare multiple variables on a single line, separated by commas:


var x = 0.0, y = 0.0, z = 0.0

Type Annotations

Annotations are used to specify the type to variables. With the help of Type Annonations it will be clear what kind of value that specific variable can store. And this can be done by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use.


var variableName: = 


var name: String
name = "Manish"

var varA,varB,varC: Float

How to use print statement in Swift?

//Playground - noun: a place where people can play

import UIKit

var name = "Manish"
var age = 23

print("(name) age is = (age)")

Output :-

Manish age is = 23

Recommended Articles

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor