Swift Variables


What Are Variables?

Variables are containers that store changeable data. Think of them as labeled boxes where you can put and update values anytime.


Declaring a Variable

Use the keyword var when you want to store something that might change later.

var age = 25 
age = 26  
  • age is the name
  • 25 is the value stored
  • var lets us change the value later

Constant Values

If you want a value that never changes, use let instead.

let birthYear = 1997 
// birthYear = 1998 // ❌ Error – you can't change a constant 

let makes the value fixed.


Type Inference

Swift is smart – it figures out the data type based on the value:

var name = "Alice"   // Swift knows this is a String 
Var score = 90       // Swift knows this is an Int 

You can also explicitly define the type:

var city: String = "Paris" 
Var height: Double = 5.8 

Mix of Types

Here’s a quick look at what you can store in variables:

  • String → words, text: "Hello"
  • Int → whole numbers: 42
  • Double → decimal numbers: 3.14
  • Bool → true or false: true

Prefer Learning by Watching?

Watch these YouTube tutorials to understand GIT Tutorial visually:

What You'll Learn:
  • 📌 Swift - Variables
  • 📌 Introduction to Swift 2 - Variables and DataTypes
Previous Next