Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          Okay then.

          So now we've seen strings in more detail.
          
          Let's shift our focus now to another datatype in JavaScript that is numbers.
          
          So we have also briefly seen numbers in action.
          
          When we looked at variables, we did things like storing age or year or a score in a variable.
          
          And they were all numbers, weren't they?
          
          Now we could use numbers for various things in JavaScript, like an age or a score or how many upvotes
          
          a blog post might have, for example, or to perform some kind of calculation.
          
          So there's loads of uses for numbers in JavaScript.
          
          So let's have a look at them in action.
          
          First of all, I'm going to create two variables I'm going to use Let for the first one, and this is
          
          called Radius, and that's set equal to ten.
          
          Now I'm going to create a constant called Pi because I don't see myself changing this.
          
          I don't want to change it.
          
          It's a constant.
          
          And that is 3.14.
          
          So Pi is a special value in maths.
          
          Used to calculate the area of a circle and the radius is just the radius of the circle.
          
          So we have those two variables which are both numbers.
          
          Let's just log them to the console.
          
          So we'll say console, dot log and then we want the radius and also pi.
          
          So let's save that and see if it works.
          
          There we go.
          
          Ten and 3.14.
          
          So these are both numbers.
          
          This is a whole number.
          
          This is a decimal number, but they're still both numbers, the same data type in JavaScript.
          
          Okay.
          
          So then what can we do with numbers?
          
          Well, we can do all the typical math operations.
          
          So let me just write these down.
          
          First of all, math operators, and that is a plus and minus multiplication, which is an Asterix,
          
          a division which is a forward slash.
          
          Then we have double asterisks, which means to the power of and then we have percentage which gets us
          
          a remainder.
          
          So let's have a look at some of these.
          
          First of all, I'm going to console dot log and I'm just going to say ten divided by two.
          
          Okay?
          
          Now we don't need these spaces that I've just put in there.
          
          I've done that.
          
          So it's easier to see and this should get us five right.
          
          And it does, it performs that calculation and then spits out the result.
          
          So that's pretty simple.
          
          Now, what if we do something like this?
          
          I'm going to say let result equal to the radius which we have at the top.
          
          It's ten and I'm going to use the percentage three.
          
          So what this does is get us the remainder of a calculation.
          
          It takes the radius, divides it by three, and then gives us the remainder, which should be one.
          
          So let's log this to the console console dot log, and we want to log out the result variable that we
          
          just created right here, which is storing the result of this calculation.
          
          It should be one that is the remainder and it is one.
          
          Cool.
          
          Okay.
          
          So now let me comment this out and in fact, I'll comment this one out as well.
          
          And let's do another.
          
          This time we'll say let result equal to the Pi, which is 3.14 times the radius to the power of two.
          
          So that is the formula for working out the area of a circle.
          
          So we're saying the result is pi r squared.
          
          This is the squared bit to the power of two.
          
          Okay.
          
          So it's the radius times the radius.
          
          So let's save that and we'll log it out.
          
          The result and this is the area 314, which makes sense.
          
          Okay, cool.
          
          So that's some simple examples.
          
          Now I want to talk about something that you probably already know about, but just in case you don't,
          
          I'm going to go through a bit and that is the order of operation.
          
          Now, this is something you might learn about in school at some point, and it just dictates how we
          
          perform calculations when they get a bit more complex.
          
          The order of operation, in what order, We do these bits of the calculation first.
          
          So you might have heard of this.
          
          Bidmas Let me just type this out.
          
          And this is the order we perform the calculations in.
          
          First of all, we do any brackets then any indices, which are these things here, then any division,
          
          then multiplication, then addition, then subtraction.
          
          So say, for example, we had some kind of complex formula, which is something like this, we'll say
          
          let result equal to and we'll say five times and then in brackets ten minus three and then we'll do
          
          to the power of two.
          
          So it's not overly complex, but this right here dictates in what order we perform this calculation.
          
          So first of all, be brackets.
          
          We perform the bracket calculation.
          
          So ten minus three is seven, then indices, which is this.
          
          So then we square it.
          
          So seven squared is 49, then division.
          
          Well we don't have any then multiplication which is this thing over here.
          
          So we say five times 49 which is 245.
          
          So that should be the result.
          
          So let's log this to the console and see if we're right.
          
          245 Cool.
          
          Now let me just comment this out.
          
          And what I'm going to do now is create a new variable and we'll say let likes equal to ten.
          
          Now imagine.
          
          This is the number of likes a particular blog has.
          
          Now, what if we wanted to add one to this likes variable?
          
          Well, we could do something like this.
          
          We could say likes is now equal to likes plus one.
          
          And if we log out likes console dot log likes, then we should see 11, right?
          
          So we do 11.
          
          But there is a shorthand version of this in JavaScript.
          
          An easier way to do this is by saying likes plus plus and that adds one to the likes.
          
          So if I comment out the first one, then this will run and it was still add one to the likes.
          
          So we should still see 11 and we do.
          
          So that is a shorthand operation there.
          
          Double plus and it's the same as adding one like this.
          
          Now the same can be done for minus single like.
          
          So if I comment this out, I could say likes minus minus and that takes one.
          
          So it should be nine and we get nine.
          
          Cool.
          
          Now what if we wanted to add ten?
          
          Well, we could do the same thing up here.
          
          We could say likes is equal to likes plus ten this time, but an easier way, a shorthand way is by
          
          saying likes is plus equal to ten.
          
          So what that does plus equal is take the current value and add on this value.
          
          So it's going to take the ten and add on ten.
          
          And if we console out the likes, so console dot log likes, save it.
          
          And we do that twice now.
          
          So let's delete one of them.
          
          We should see this in the console we see 20.
          
          Cool.
          
          And the same is true for other things like minus or times or divide.
          
          So let's do some other examples.
          
          I'm going to comment this out and this time I'm going to say likes and minus equals five.
          
          So it takes five away from it.
          
          So let's save that and we get five left.
          
          That's right.
          
          I'm also going to do a couple more examples.
          
          So we'll say likes and we'll say times equals two.
          
          So that is going to take this value and times it by two, which is 20 and we get 20.
          
          And then finally, let's do divide.
          
          So we'll say likes divide equals two and that takes the value ten and divide it by two.
          
          So we should be left with five and we are cool.
          
          So this here, this is all shorthand notation.
          
          It's stuff we can do the long way.
          
          Like this we say likes equals two likes times two or plus two or whatever.
          
          But this is the quicker way of doing it right here.
          
          And when it's just one we can use plus, plus and minus minus.
          
          Okay.
          
          So that's going to speed things up when we're doing little calculations in the future.
          
          All right.
          
          So just a couple more things I want to talk about.
          
          First of all is not a number or nan for short.
          
          So it looks something like this.
          
          N a n stands for not a number.
          
          And we get this value when we try to do something that doesn't make sense, some kind of calculation
          
          that doesn't result in a number.
          
          For example, if I log to the console five over Hello, that just doesn't make any sense whatsoever,
          
          right?
          
          We can't do that in real life.
          
          And if we do that in JavaScript, we get Nan or Nan.
          
          It's not a number and we can do something else.
          
          We could say console, dot log and we'll say five times hello and log this to the console and we get
          
          not a number.
          
          Surprise, surprise.
          
          Okay, so that's one little thing.
          
          If you get that, it means you've tried to do some kind of calculation that doesn't result in a number
          
          and you've probably made some kind of error along the way.
          
          Okay.
          
          Now the final thing I'd like to show you is how to concatenate numbers.
          
          So, you know, like we said, with strings, we can concatenate them with a plus sign.
          
          Well, we can do a similar thing with numbers If we're trying to add some kind of number into a string,
          
          we can do that.
          
          So let me give you an example.
          
          I'm going to say let result equal to the blog has and then a space and I'm going to concatenate this
          
          with a number.
          
          This thing right here likes so plus likes.
          
          Then I'll concatenate with another string space likes.
          
          So this should say the blog has ten likes and it should result in a string.
          
          So it takes the ten and it adds it into the string and it creates one big gigantic string here.
          
          Well, not gigantic, but you get the point.
          
          One big string based on all this.
          
          So if I now say console dot log, the result, we should get that.
          
          Let me view in a browser.
          
          The blog has ten likes.
          
          Okay, now this is fine.
          
          But if we were to do this with a few different numbers and we had quite a big string, then this would
          
          quickly get pretty messy when we're trying to concatenate too many variables with a string.
          
          Now, fortunately there is a new way to do this using template strings and we're going to see that in
          
          the very next video.