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

Using Variables

So, we have variables and we know how to create them but how can we actually use them and for what? We agree, it's time to see them in action.

The earlier videos explained what a variable is, but the discussions were brief and limited to initial declarations. Now, we will see how to use variables in the context of Flash. Basically, a variable doesn't belong to any specific data type. We can assign any type of data to a variable but we must ensure that the variable is declared initially using the same data type.

The most important feature of variables which makes them so popular is their re-usability. Variables are just like containers in which anything can be saved. For example, imagine that we need to perform multiplication on 3 sets of numbers containing 3 integers each: [(1,2,3)(2,3,4)(7,8,9)]. Now to multiply these we need to write the trace line for each set.

Instead, if we use the variables (a,b,c ) and assign the first set of values to these variables then we can perform the multiplication of the first set using abc. Now, for the multiplication of the second set, nothing much changes but we assign the values of the second set to a,b and c. This way we can reuse the variables any number of times and reduce the length of the code.

Instead, if the values that are being operated are continuous numbers, then we can use the property of incrementing and looping with the help of variables which reduces the bulk of the code to a single loop. When there are many values to be calculated, using variables is the best option.

The use of variables is not just limited to numbers; strings can be assigned to them as well. We assign a string to a variable at the beginning and can perform any needed modifications on it. Later, if we want another string to be used instead of the present string, there is no need to rewrite the whole program. We just change the string that is assigned to the variable. Moreover, addition of strings seems difficult when we output the sum directly. Using variables for the addition of strings is the best option.

Video Transcript

00:01Let's delete our traces again, and let's look at a sample. That's a bit more advanced. Yeah, I'm going to delete only the
00:08this
00:10Descriptions or the need I'm going to leave the 2 plus 2 and a low world and let's go into variables
00:14We've seen the variables before we haven't really used them, so I'm going to create your variable, and it's going to be a num
00:22Which is a number?
00:23We could then
00:28Put a value inside of our number which is 2 now
00:31We put a different type of value like we so like we've seen before would generate an error we have to
00:37We have to input a number to our variable because we defined it as a number type
00:44So now that we have a num that is a number type
00:46We could actually trace num plus num or num plus 2 and num plus 2 would be the exact same thing as
00:532 plus 2 only instead of it being a constant instead of it being a value
00:58That's predefined we could change that value to whenever we want to so if now if for example
01:04We wanted to find out what would be
01:06the squared value of num
01:11So we could have gone we could have typed instead of that num times num
01:15To create our square value of num and if we trace this out now
01:20We're going to see that our value would be 4 before but if we would go to our num and change it from 2 let's say
01:26to 10
01:27We don't have to change anything in our trace anymore, and we're going to get the square value for 10, which would be 100
01:37squared value of 10
01:39Okay, so now you're starting to see the the value of working with variables because variables are reusable
01:46And it makes it very easy for us to switch around
01:49content without a lot of changes and
01:53The same thing goes for strings, so if I had your string
01:57Then its value would be hello
02:06Okay, so I'm calling it str
02:08But usually you'd want to name your variables and names that describe your your input so for example if we know that this will always
02:15Be a low then we would maybe call our variable even
02:19Hello, but one one common way of calling variables just so we could define them and differentiate them
02:25So we'll remember between if it's a number if it's a string if it's a different type which we're going to talk about in
02:31later videos
02:32So now that we we got our we got our numbers down and our strings
02:38And we want to figure out a way how to differentiate between them and as we add more variables
02:42There's a system and basically the bull area in system which it's naming doesn't really matter as you can see
02:47I don't even remember myself the naming is instead of naming a variable just randomly whatever you want
02:53we're always going to start it up with a
02:56Character or two characters that will help us define it and help us know
03:00What type it is for example? We know it's a strings. We're going to start our variable with s
03:05And then we're going to camel case it which camel cases means we're going to camp slot. We're going to uppercase
03:11every
03:13First letter of every word that we're writing so for example it could be s message
03:19And our s message is going to have
03:22This copy. Hello, and maybe we even going to say we're going to put here a comma
03:26So here's our s message, so let's try to use our s message. We can now take our s message and delete. Hello
03:34Our string hello make sure we were doing deleting all the you know the double brackets from start to end
03:39And we're going to paste our message. Hello right inside
03:43And what should come out right now if we paste our application would be whatever is inside of my message
03:47Which is hello comma then we're going to have a space and then a world
03:52And if we run the application one more time to see if it works
03:57We're going to see that it's exactly outputting what we would expect it to output. Let's create another variable
04:04And we'll create another string which we're going to call s name
04:08Which is going to be a type of string and we're going to put my name in it or your name in it
04:19You see so now we're starting to kind of automatically type it and not repeat so every time we're adding a string
04:26We're putting in a double quotation or a quotation. We're kind of taking this under
04:31Assumption that you're you're already comfortable with it
04:34And if you're not then take a pause start playing with that again play with the numbers
04:38Play with combining numbers and strings and strings and numbers and seeing what happens
04:43And that's the best way to figure out how to work with
04:47The basic numbers and strings which are very very basic fundamental
04:52Stones and flash so now if I wanted to change the world
04:56Copy I could grab my string. I could grab my variable, which is a string and I could paste that variable and
05:05swap my
05:07String that had a low in it. So now a low and
05:10Excuse me a world was in it. So now I have a complete message, which is dynamic with a space. That's hard-coded
05:18What I'm saying dynamic is because we could change this value and it would automatically change every place inside of our function
05:24That had this variable
05:30Perfect so we could see that we have here a low
05:33Comma Ben and it's all a combination of strings
05:37so in this this video we've we've looked a bit deeper into combinations and
05:43What I would recommend at this point of if this topic is clear
05:47Then just move on to the next one the next video and if not
05:50Then stop and start playing around with these values and just get familiar with them play around with basic math play around with
05:58Combining dynamic strings such as
06:01Just writing a simple paragraph
06:04With a few names that are changeable or phone numbers that are changeable or just something that you could see so you could start seeing
06:11The power of using variables instead of hard coding values

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!