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

Deeper Look: Defining Variables

Now that we know what variables are, it's time to look deeper into their structure and how to play with them. Also, we will learn about a new error type we can now check to avoid: Error 1120.

Declaring and assigning variables is not new to us. We have discussed variables in almost every other video. However, we didn't dig very deeply before, stating it would be discussed later. Now it's the time to know everything about variables and look more deeply into them.

First of all, to declare a variable we need the keyword var. For example, var a; Here, 'a' is the variable. Earlier we have seen variables declared along with their data types. But here we can't figure out what type of data the variable is going to store. In the same way, Flash also can't understand the data type that is going to be used. In this case, if we assign a string to the variable a, Flash takes the data as string. In the next line, if we again assign a value to 'a' of data type Number, Flash doesn't show any error but takes the integer value as well.

Here we need to understand one thing :i Flash generates an error only if the assigned data type doesn't match the defined data type of the variable. Above, we didn't define any type for a. Ergo we recommend defining the data type during the declaration of the variable: var a:int; will do it. Now, if you happen to forget a is an integer and write a="uh oh this is a string!"; Flash will puke! Assigning a type to a variable at definition will make the application run smoothly and it helps us as well while writing the code. You must declare a variable along with its data type and assign the same type of data to it.

Single Command in Single Line

The next thing regarding variable definitions is the necessity to end the declaration with a semicolon. The application considers a line to be ended only if the semicolon is present. If there is no semicolon, Flash considers the next line as part of the first line's code – until that semicolon is reached! Think this could cause an issue of two? A bunch of errors will appear. But if you want to use the same command for declaring two different variables (a:String b:Number;) then the semicolon should be specified at the end of the second statement. These two statements can be written either on one or two lines. Flash considers it to be a single line until the semicolon is reached. Although ActionScript is a forgiving language and many times will automatically end the line on its own, it's good practice to always end a line with a semicolon as other languages aren't as forgiving.

Video Transcript

00:00Now, another item I want to talk about when we're talking about variables is that we have
00:05to define them.
00:06If we try to work with a variable, such as let's say e, and try to put a value into it,
00:11we never define that variable.
00:14And because we never defined it, if we try to run that application, it's going to give
00:17us an error telling us access to an undefined property e, which basically means that we
00:22did not define this variable.
00:25Flash does not understand what that is, it didn't store room and memory for that variable.
00:33Let's look a little bit deeper into the way we build our variables.
00:37So we said we have to state the word var, and then put a space, and then define our variable.
00:44We could have if we wanted to end it in this place, and just create our variable e.
00:49Now in ActionScript 2, that's completely legitimate, but in ActionScript 3, it's preferable not
00:55to do that.
00:56Because it would waste Flash's energy trying to figure out what that variable is, and at
01:02the same hand, it would make our development much harder, because then it would be hard
01:06for us to know if there's a number, or a string, or a different type of value in our variable.
01:13And if we don't know what type of variable we have in there, the compiler as we compile
01:17will not be able to give us accurate errors.
01:20So if I publish our file right now, we'll see that I'm not getting any errors right
01:24now, because I'm not in a strict mode.
01:26But in different modes, we're going to get not errors necessarily, but alerts telling
01:34us that our variable wasn't defined properly.
01:36But in this case, we're not getting any alert.
01:39And that's a problem, because if I go into my variable e, and I put into the value 3,
01:44and then the next item I put inside of it by mistake a string, such as my name, and
01:49I run the application, there will be no error.
01:53Well if I would tell Flash that I want only numbers to be inside of this variable, and
01:59I run the application again, Flash will be smart enough to let me know that I've made
02:03a mistake that I've put a different type of value than I've wanted to into the variable
02:07e.
02:08So if I run the application again, it's going to prompt me, and tell me implicit correction
02:13of a value of type string to an unrelated type number.
02:17OK, so it's very important for us to state what type of variable we're working with.
02:24Let's look at another few options with working with defining variables.
02:28Now we could define a variable, such as e as a number.
02:32And if we wanted to, we could have typed comma space, and added another variable, even
02:38of a different type, such as s as a string.
02:41And that's completely legitimate, and when we're done, we could end up with a semicolor.
02:47Now with that said, it's not really recommended to work this way.
02:50One, because it's not very readable, and we want to make our code as legible as we can.
02:56And two, it makes it, it's a very, very minute problem, but it makes Flash work a little
03:00bit harder by us not giving Flash one command per line.
03:05And basically, we're asking Flash to do two things in one line, and preferably it's
03:09better off always to tell Flash to do only one thing at a time.
03:13We always end our commands, and when we tell something to Flash, we always end that
03:18command with a semicolon, kind of letting Flash know that this is the end of the line.
03:25Now, if you don't put a semicolon, Flash is smart enough to silently figure things out.
03:32But it's a standard, and a lot of languages request you to add a semicolon, so
03:37try to make sure that you always add a semicolon and an end of a line.
03:40So we've seen how we could add multiple variables in one line using one var
03:46definition, and we said we don't really recommend it.
03:50So for example, another way to illustrate why we do need the semicolon is,
03:56for example, we could say, we could type our variables this way as well.
04:01We don't have to define a new var, and this is legitimate.
04:04This is all considered one line for Flash.
04:07Flash is going to deal with this at one time.

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!