Variables in Swift

Variables in Swift

What are variables? Most of you are here to understand the first and foremost concept of programming, but we will help you answer with the simple steps:

If you were attentive and loved Maths as your subject in High School, it's easy to identify this concept quickly.
Never mind, forget that Math Class from your boring instructor. We are here to explain it in an easy way. For that, speak out the word variable. Does it sound somewhere close to the word "Vary", well now we assume you know the meaning and would have guessed the definition of the term "variables".

Variables are one of the concepts in programming and not just in Swift. It stores some value whether it may be an Int, Float, String, Double, etc.

If you are new to Swift or programming, we suggest you go over the upcoming article to understand Data Types (Int, String, Double, etc.)

Also, we highly encourage you to implement it with us by running the Playgrounds app by Apple. You can download it from the App Store. If you don't have Mac but an iPad, it will run on that too!

Let us now declare a variable. To declare a variable in Swift, we first write down "var". You can say it's also a short form of the term "Variable". If you see the color of the keyword we entered, it would be the pink one. After that, you write the name of your variable, which you can see in the following examples:

var name
//or
var age

Congratulations! You have declared a variable, but to give/store a value. Follow the last step.

The last thing we do is give them the value, like

var name = "Krishna"
var age = 18

Since the variable "name" is a String, you have to add a quotation mark. Also, we know that constants don't vary, but variables do. So, if you want to change the age to 72, it's easy to do that (not in real life).

age = 72

This time you don't write that keyword "var" again, write the variable name "age" or whatever you declared, and add the new value to it!

What's next?

It's beautiful to start a journey but don't stop here, push yourself to learn other concepts. To understand the following concepts in order, you must be understanding the concept of constants next. Have a great time ahead!

Eat. Sleep. Swift Anytime. Repeat