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

Working with Operators

Hopefully you're starting to see the power of variables. So far we used variables as if they were constants but that is about to change. In this lecture, we will see how you can change variable values throughout an application. This is really one of the most powerful features of variables.

Video Transcript

00:00Hopefully you're starting to see the power of variables, but so far we treated variables
00:05as if they're constants.
00:06And when I say constants, what I mean is that we didn't change them, we set them once
00:10and then we used them, but we never redefined them and never did anything to manipulate
00:15them.
00:16But that's all changing in this lecture where we're going to take our number variable
00:20and see how can we change it throughout our progress of our application to create more
00:25custom solutions.
00:26And really that's part of the power of variables that make it different than constants.
00:32So let's see what that actually means.
00:34Let's jump right in.
00:35We treated our variables as static things.
00:38We only used them once.
00:40Well, actually we used them three times here, but we didn't change them.
00:44We only changed them once in line 15 here.
00:47I want to show you how do you actually update and change your variable midstream.
00:52So literally I could go at any point and I'm just going to break the lines here and
00:56I could go ahead and go to my variable and redefine it anywhere I want to.
01:01So I could say, Hey, you're, you're equals two right now.
01:05Now what this will do is very simple from here, all the way until the change happens,
01:11the value will be whatever value we set.
01:14In other words, this tween will take one second, but now that we changed the value
01:19in line 21, everything below that will automatically take the new value of the variable.
01:25In other words, this will take one second to animate while these two elements will take
01:30two seconds to animate.
01:32So if I save and go back and refresh, we'll see that first of all, that let me just also
01:39set here going into my element here, I'll send my opacity back to one because it's
01:43not very clear.
01:45And you know what?
01:46I'll put the animation even to take longer.
01:48So I'll set it to five.
01:49So it'll be very clear the difference between the two.
01:52So we'll see one finishes really quickly while the other two will animate for five seconds
01:56until they're fully in the screen.
01:59So that is one way of us updating a variable and we can update a variable at any point.
02:06Now we don't have to only put static values into a variable.
02:11We could refer itself into the variable.
02:13In other words, if I would set here the value of anim time plus five, then what I'm actually
02:23going to get here is the original value of anim time, which was one plus five, which
02:29this together equals six, and all of this will be assigned into our anim time.
02:35In other words, our new anim time will be six seconds.
02:39So if we run it again, you won't see a very big difference, but the top area will take
02:43one second to animate while the bottom two elements will take six seconds to animate.
02:59So that's one way of working with a variable and redefining it.
03:04So I want to show you also another something.
03:06So and instead of plus five, I'm going to set it plus one.
03:09So it's not going to take that long and I'm going to go ahead and break a line in between
03:14the next animation.
03:15And again, I want to do another change to it.
03:18So I'm going to go ahead and I'm going to set my end anim time equals just like we did before.
03:25And I'll do L equal to end anim time times two.
03:33And literally, again, just like with the plus value, the times two, and I could put space
03:37anywhere I want just to make it visually more pleasing.
03:40Space doesn't mean anything.
03:42If you're not inside of a double quotation, it just means spacing for us.
03:46So literally, what that means is that first animation will take one second, our second
03:50animation will take two seconds because it's one plus what it was originally.
03:54So that equals two.
03:55And now anim time equals two.
03:57So second animation will take two seconds.
03:59But an anim time in line 26, we're taking the original value, which was two, duplicating
04:04it by two, basically saying that it's going to take four seconds.
04:08So if I run my animation, this will take one second, two seconds, four seconds.
04:13And if I save this and go right back in there and click on refresh, we'll see one, two seconds,
04:19four seconds to get it.
04:21And to make this difference even more sharp, let me just comment this out.
04:25And I'm just going to copy the same rule up here just above here just to see how powerful
04:30that is.
04:32And literally, what will happen is this will be one second, this will be two seconds,
04:36this will be four seconds, so not that big of a deal, right?
04:38So how about we do times four and times four.
04:43And if I save this will be one second, four seconds, 16 seconds.
04:46So it's going to be a very major difference.
04:49So one second really quickly, four seconds to get this baby in.
04:53And this will take 16 seconds all the way until it'll be completely there.
04:57We're not going to wait for the complete load.
04:59So talking about that, so when we're talking about variables, you could have plus, you
05:04could have minus, you could have, I'm just going to put them all right here.
05:09So we could have plus, we could have minus, we could have divide.
05:12We could have times, and there's a few other things such as moldo, which we're not going
05:16to get into in this title.
05:19But moldo enables you to get the leftover.
05:22So if I take two moldo, or let's say three moldo two, excuse me, the value that would
05:29come back would be one, because that's going to be the leftover if you divide by two,
05:33leaving out the full values.
05:35Or for example, five moldo two, again, will we give back one, because there's going
05:42to be a leftover.
05:43But we're not going to worry about that.
05:45For now, just to get familiarized with plus, minus, divide, and times.
05:51And that's your homework right now.
05:52And in the next lesson, we're going to continue seeing how do we get a little bit of user
05:58interaction, which we're going to see in the next video once I stop coughing.
06:02But in between, how about you come up with your own set of animations and try to figure
06:07out what combination of variables.
06:11What combination of variables and playing with their values will create the coolest
06:15animation you can create and share it with us here on the comment section.
06:18I would love to see your interpretation, your animation already with the skills we've developed
06:24so far.

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!