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

From Constants to Variables

In this lecture we will meet our first variable of number type. We will animate three items on our stage and control them all with one variable.

Video Transcript

00:00In this lecture, we're going to solve our homework from our last lecture, which I hope
00:04you finished.
00:05Now, if you didn't, it's fine.
00:06We'll do it together, but I do hope you did do it, and if you didn't, you still have
00:10a chance to pause.
00:11Go ahead and do it.
00:12By the end of this lecture, not only will we finish that homework, we'll also meet
00:16our first number, our first variable.
00:19So let's get started.
00:20All right, so let's go ahead and continue basically our process.
00:26So to do that, I'm just going to copy ahead, and I hope you did your homework again, and
00:30I'm just going to paste that animation twice.
00:33All that's left for me to do is to change the actual content in the first property that
00:37defines what we're animating.
00:40And in this case, I'm just going to go to our CSS and copy the rules that we want to
00:44animate.
00:45So that was our paragraph, and we're going to set that as our second item that we're
00:49animating.
00:52And I'm going to go ahead and find our H2.
00:55I'm going to go ahead and copy that and send that in to our element to animate.
01:00All right, so now that we have all three items, if I save this and go back into my browser
01:06and click on refresh, you're going to see that all the three items are animating at
01:10the same time.
01:11We have your little mistake, a little problem.
01:14So if we go back into our code to try to find what is our problem here, we'll see
01:18that I'm basically animating the same thing twice.
01:20But what I really wanted to do was find the actual area where our image was and animate
01:25our image as our third property.
01:28So let me just paste that there, save it, go back into my browser and click on
01:32refresh.
01:33And now we should see all three items fading in at the same time.
01:37Last but not least, we said that we want to change the duration of each of our
01:40animations.
01:41So we said the first item will take a second.
01:44The second item will take a second and a half.
01:46And the last item we said will take two seconds.
01:49And if I save this and I go back into my browser and click on refresh, now we'll
01:55see that there is some sort of a transitional effect where our items are fading
02:00in, but they're not all fading in at the same time, not completing fading in at
02:04the same time.
02:06Although this wasn't very drastic, if you want to see a more drastic effect, then
02:09just notch it up where you have a two second animation and then a three second
02:13animation.
02:14And if we click on refresh, we'll see that now our animations are, you know,
02:18there's a longer flow to the animation itself where this kind of lingers a
02:23little bit longer.
02:24So there you go.
02:25That was our task of creating our animations.
02:28Now, I actually want all my animations to take the exact same duration, but I
02:34might want to change the duration over time.
02:36Wouldn't it be nice if I could create here some sort of an item and then
02:41just say copy that item value into all these properties instead of putting
02:45here one and imagine that we put here also one and we put here also one.
02:50And how about also we have your our opacity.
02:52Maybe we want to change the opacity, but each time we want to put a different
02:56variable, a different, there you go.
02:58I just, I just dropped the name.
03:00That is the name is called a variable.
03:02So let's see what that is.
03:11First of all, the way we create a variable is by typing the word V A R and
03:16that stands for variable.
03:18The next thing that we're going to do is we're going to give a name that
03:20is very descriptive to our variable.
03:23For example, if we want to say the animation duration.
03:27So how about we call it anim for duration?
03:30Now, one of the classic ways of defining variables, especially in JavaScript,
03:37we're going to learn in the next few videos about a few different
03:39types of properties, now variables that is, and we want to be able to
03:44differentiate between the different types.
03:46We said there's strings.
03:47We've seen these strings and we said there's numbers.
03:50So how about we start everything with a Boolean indicator or
03:53Bulgarian indicator, I believe it's called, and I'll just type here the
04:13letter N for number, and then I'll write anim time.
04:18So basically that will tell me that anytime I look at this variable, I'll
04:22be able to see that, oh, this is a number and the number
04:26represents animation time.
04:28Now notice how I camel cased all the secondary letters.
04:32I didn't put a space because a variable cannot have space.
04:36Although you could put special characters, I just don't recommend
04:39using special characters.
04:41Just don't because it looks weird.
04:44Just keep a constant only characters, only Latin characters, and just
04:48have it camel cased, always starting with a lowercase letter.
04:52That's our rule.
05:10In general, I would say the first letter should represent what type
05:13of information is in there and the name should describe really well what it is.
05:18You don't have to worry about how big it is.
05:20You could write a variable name as big as you want to make it
05:23easy for you to decrypt.
05:25Now, once you set a name to a variable, you'll want to probably set a value into it.
05:30So you'll put the equal sign and just set a value into it.
05:35For example, in our case, we want to set the variable to have the value of
05:40one, and then we put a semicolon.
05:42Now that is the process of creating a variable and defining its value.
05:48Now that we created a variable and defined its value, it's time for us
05:52now to also go ahead and use that variable.
05:56So I'm going to go ahead and just copy this end time, and I'm going to go
06:00ahead and just paste it where our animation time is now by doing this.
06:06Nothing will really change besides the fact that if I save this, it's all
06:10not going to have a static value or a constant value inside of a second
06:16property here of time, but instead of that, it's going to have a variable
06:20that we could control from above.
06:22Now, once I saved it and I go into my browser and click on refresh, we're
06:25going to see that all the animations take exactly one second to load in.
06:30But the power of using a variable and the first power that we're going to
06:34see is I could very easily in one line of code now go ahead, go ahead
06:38and change all of our animations to take 10 seconds.
06:42And now if I just, just by changing one line here, because I put
06:45inside the variable in all the different positions where we want to use it,
06:49I could go ahead and click on refresh.
06:51And we'll see that our animation now is going to take a whole 10 seconds
06:55to animate in, and we've done that literally using only one variable.
07:01Now I want to give you a little bit of homework before we move on into
07:04our next lecture and the homework I want you to do right now, put a pause.
07:09I want you to create another variable and define anything in here.
07:13So you could, or define one of these elements as a variable, or how about set
07:19your opacity and set a variable for the opacity as well, setting here another
07:25variable in the next video, we're going to solve that task, that homework
07:29work, homework, work, work, and we'll get familiarized with our second
07:34variable, which will be a string.
07:37So we'll see you in the next lesson where we will solve that homework
07:41lesson I just gave you, and we'll see a new variable.
07:44And please do comment your answer into the comment section, because it's a
07:48great way for you to get feedback from me and from other students,
07:52if you have a mistake.
07:53And in any, anyways, it'll create a lot more interactivity.
07:57Learning passively on your own is much harder than engaging.
08:00Engaging is one of the best ways.
08:02Interacting is one of the best ways of learning.
08:05So I'm here in the chat room.
08:06I'm here in the commenting, I'm here in email support as well.
08:09And many of our students are in the same video as you are right now.
08:13So if you have any question, write them right down there.
08:16But in any case, I hope to see your homework assignment just pasted
08:20right into our comment section.
08:21So there we go.
08:23See you in the next lecture when we solve our homework of creating
08:26a new variable, one of your own.
08:28See you in the next lesson.

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!