Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          Okay then.

          So we've seen quite a lot of different types so far.
          
          Now I'd like to touch on something called type conversion, and that basically means turning one data
          
          type into another data type.
          
          And you might want to do this sometimes.
          
          For example, we've got a variable here called score and we've set it equal to 100, but the string
          
          100.
          
          Now imagine we wanted to perform some math with that value.
          
          We wanted to add one to the score, for example, which would be 101.
          
          Now if that's a string and we try to add one, then this is not going to work.
          
          We're going to get concatenation whereby the number here is added on to the end.
          
          So it becomes 1001.
          
          So we're not adding one to the value.
          
          So it would be nice if we could convert this string into a number type and then perform the calculation
          
          that is type conversion.
          
          So then let me do this.
          
          What I'm going to do is grab the score value and we're going to update it by turning it into a number.
          
          And the way we do that is by saying, number capital N, and then in brackets we pass in the variable
          
          that we want to turn into a number, which is the score.
          
          So now we're saying the score is equal to the number version of the score and that takes this string
          
          and it turns it into a number and that number would be 100.
          
          So now when we try to add one to the score, because it's a number now, we've converted it, now it
          
          should be 101.
          
          So let's test that out.
          
          Now we get 101.
          
          Cool.
          
          Okay, so that's a simple type conversion.
          
          And by the way, you can check the type of something by using the type of operator.
          
          So let me just do that console dot log and I'm going to say type of all one word, then a space, then
          
          the variable that we want to check the type of.
          
          So we want to check the type of score.
          
          I'm going to save that and we're going to see that this is a number.
          
          Now, if I just comment out this line so we're not turning it into a number, then it should be string
          
          because originally it was a string up here.
          
          Okay.
          
          Okay, cool.
          
          So let's move on now.
          
          What if we have a new variable called results and we want to set that equal to a string?
          
          What if we want to try converting this into a number?
          
          Well, that doesn't really make sense, does it?
          
          Let's try it.
          
          So number.
          
          And then we pass in the value that we want to turn into a number.
          
          I'm going to log down here.
          
          The result like so and save it.
          
          So if we come over here, we can see now not a number.
          
          So if we try to take something that doesn't really make sense to turn into a number and try to use this
          
          right here to turn it into a number, then we're going to get not a number as a result.
          
          Okay.
          
          All right, then.
          
          So let's try it the other way around.
          
          What if we want to take a number and turn it into a string?
          
          Well, let's say let result equal to this time string with a capital S, and then we're going to pass
          
          in a number.
          
          I'm going to pass in 50 and I'm going to save it and come over here and we can see 50.
          
          Now, how do we know that this is actually a string and not a number?
          
          Well, first of all, when we type in a number in the console, it comes out as blue.
          
          But that's shady logic.
          
          We can use the typeof operator down here, we can say typeof and then we'll put the result there.
          
          So every time we do some kind of conversion, we're outputting the result and the type of that result
          
          variable.
          
          So if I save, we can see that 50 and string and that's what we wanted to do.
          
          We wanted to take this and convert it into a string.
          
          Right?
          
          Okay.
          
          So let's move on.
          
          Let's do another example.
          
          We can also convert things into booleans.
          
          Remember booleans.
          
          Are those things true or false?
          
          So we could say let result equal to boolean and then pass in something like 100.
          
          Now what do you think this is going to do?
          
          Well, if I save this and come over here, we get true and the type is Boolean.
          
          So when we do this type of conversion in JavaScript, when we try to make something into a Boolean,
          
          some values are considered true and others false.
          
          Now you might hear them being called truthy or falsy values.
          
          And this right here, this 100.
          
          This is a truth value.
          
          But if I try zero and save it, then we get false.
          
          So zero is a false value.
          
          Okay, so positive numbers are considered a truthy value in type conversion minus numbers are also truthy,
          
          but zero is false.
          
          Okay.
          
          Now let's do another example.
          
          I'm going to come down here and say again, let result this time equal to a boolean and it's going to
          
          be the string zero.
          
          So what's this going to be?
          
          We know that this is a falsy value, normal zero, but what about a string of zero?
          
          Well, let's come over here.
          
          We can see this time it's true.
          
          Now, strings of any length.
          
          They are all truthy.
          
          But if we make it an empty string of no length, then this would be falsy.
          
          Okay, we can see false now.
          
          So this is all explicit type conversion, meaning we've explicitly tried to convert the types right
          
          now.
          
          There's also something called implicit type conversion, which JavaScript does behind the scene.
          
          We saw that in the last video when we talked about loose comparison JavaScript changed the type of our
          
          values behind the scene before comparing them and that will come into play in other areas too in the
          
          future.
          
          For now, though, it's enough just to understand that we can convert types either ourselves explicitly
          
          like this or implicitly behind the scene.