02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

What Are Variables?

Developers use variables to save and store information that they want to access and reuse in the future. Learn how to create different kinds of variables and see how they can make your life easier as your code gets more complex.

Dealing with variables is one of the basic foundation blocks of programming. Introduction of variables into our programming solves many complications and helps us minimize code. The main role of the variable and its usage is discussed later when we start working with functions. For now, we just need to understand what a variable basically is and its main usage.

A variable holds data the way a pot holds spaghetti noodles. You could cook one at a time: we can't think of a reason why, but you could! Same goes for variables: they contain data instead of noodles. For instance, a variable (such as strData) is a perfect alternative to lengthy data such as a string. For example, if we want to print a string many times within the code, we may find it difficult to type it again and again especially if the data is comprised of many characters. In such cases, we can assign that data to any variable, use it in any part of the program and the compiler sees that variable as the real data that was initially assigned to it.

Hence, the code length can be reduced drastically with the use of variables. For example, take the string "Learning AS3 at 02geek.com is very easy." For some weird reason, we must use this multiple times in our program – say 100 times -- just to discover that we had a small typo in every single one (oh dear)... If we had one variable that contained this string we could have simply updated that variable and in so doing, updated each spot where that variable was used. So we should assign this data to a variable, say 'go02str': go02str now refers to the actual sentence wherever it is used in the program later.

The need for variables becomes mandatory when we use functions. Functions are declared first and can be called any number of times within a program: functions often have variables as arguments. For now we must remember that a variable can replace a lengthy string and can be used multiple times within the program. In the following videos we will be mainly working with variables, so don't worry if its not 100% clear yet: there's nothing like some real world practice!

The most important knowledge you should retain from this video is the understanding that variables are containers that contain data (such as our string) and can be used in place of that data, once they are defined.

Video Transcript

00:00We've learned a lot of a lot of stuff so far. We've learned a bit about numbers. We've learned a bit about strings
00:06strings as we've said earlier are
00:08Rappers were inside of them. We put text and that text is a string now. Why are we calling them strings?
00:15We're calling them strings because in flash
00:18We could store those values and reuse them more than ones
00:22You know traditionally if you for example wanted to create another hello world
00:29sample like I'm doing here where I'm tracing a little world five times you would have needed to type trace and open a round bracket and
00:39Open a double quotation and type hello world
00:43and close your double quotation and close your bracket and
00:47semicolon and grab that
00:49Maybe copy it and then paste it and paste it and paste it until you're happy with the amount
00:56And I didn't write it correctly, which is a perfect sample. So now that I didn't write it correctly
01:02I have to go through everything again
01:04hello world
01:06world which happens to be quite often where I spell something incorrectly and
01:12Now I need to save it and I need to publish it and although it's not that hard to do it
01:17It's very repetitive and as our applications grow and become bigger and bigger
01:21We're gonna have to have a way to reference the information without typing it over again
01:26And again and for that variables were created variables
01:31if I would take it into
01:33The real world would be like business cards
01:37If you would have to write down a business card every time someone would approach you
01:42It would be quite difficult and because of that instead of creating your a business card every time someone comes and asks you for a business
01:49Card or whenever you want to give someone one
01:51You just create you create a template for them and you create 500 of them
01:58you have one that's stored you have saying that you could reproduce very easily and
02:03The concept of variables is not that far away from that
02:07So the concept of variables enables us to save information into a bucket
02:11So we could reuse that information and more than reuse it we could access it in the future without knowing
02:18Specifically what's inside of it?

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!