Number Types and Math0:00
Alright, let's take a look at numbers, big ints, and some of the operations you can perform on them. So numbers can be whole numbers, let's make a variable here called num1, let's make it 4, let's make another one called num2, and make it 2. They can be decimal numbers, also referred to as floating point numbers, so anything with a decimal. They can be negative numbers, so if you add the negative sign in front, that should be fine. And you can perform any basic math operations with these numbers. So let's console.log num1 plus num2, pretty straightforward, that should be addition.
You can also use command / to toggle a comment on a line, you can do multiple lines as well. Let's say print our results, and again I'll do command / here, and you can select multiple lines and do command /, or you can do a multi-line comment with /*, and then to close it out, we can do */, or /*. So again, any commented lines won't be run, and it's just for the developer. Often times he'll also comment out lines, so that line won't get run. So let's describe this block as well. Let's say variables. Also note that sometimes if you're performing math with floating points, or decimals, you
Floating Point Inaccuracy1:46
Let's say variables. Also note that sometimes if you're performing math with floating points, or decimals, you might run into some unexpected results. So down here, let's say floating point inaccuracy. So if I console.log, the classic example is 0.1 plus 0.2. And another example is 0.2 plus 0.4. If I save this, you'll see the result is correct, but slightly off. So I won't go too deep into this, but it has to do with how computers compute floating point numbers. If you ever work with money, for example, you'll often get told to work in cents, so
point numbers. If you ever work with money, for example, you'll often get told to work in cents, so you're always working with integers and whole numbers, so you can avoid this inaccuracy. Again, just make sure you're aware of that. Let's take a look at a few other operations. There's the modulo operation. Let me just move this up. Let's say console.log. Let's just use actual numbers in here, say 13. And the modulo operation is a percent sign, 5.
Increment and Assignment Operators3:26
So increment and decrement. So this involves changing the variable. Right now it's a const, so it won't work. So let's say num1 equals the current value of num1, which is 4, and say plus 1. Let's try that out. console.log(num1). Again, this won't work because it's a const, and we get that error. So let's change num1 to a let, also num2. Okay. And now this should be 5 because we're adding 1 to it.
Okay. And now this should be 5 because we're adding 1 to it. Okay. So you can do the same thing here with the ++ operator. So I'm going to comment this line out. Let's say num1 ++, and it's basically the same thing. So if I save this, it should still be 5. The same applies for decrementing. So let's say num2 equals num2 minus 1. That should be 1.
So let's say num2 equals num2 minus 1. That should be 1. Let's change the console.log to num2, and it's 1. Or you can do num2 minus minus, num2 minus minus. Okay. And that does work. Now, there's another operator you can use if you're incrementing or decrementing with another number other than 1. So plus plus and minus minus only work for 1. But if I were to do, let's comment this out again, num1 equals num1 plus anotherValue,
So plus plus and minus minus only work for 1. But if I were to do, let's comment this out again, num1 equals num1 plus another value, say 2. So this would be 4 plus 2, and it's 6. Let's delete this. Back to num1 here. That should be 6. Okay. Or you can do the short form, which is num1 plus equals the number plus equals 2. And that should be 6 as well.
Or you can do the short form, which is num1 += number += 2. And that should be 6 as well. And you can do -= as well. So num2 -= 2. So that should be 0, because num2 is 2 to start. num2. Cool. And I believe you can do it for *= and /= as well. And I'm just going to comment out all of these because I still want num1 and num2 to be the original values.
Comparisons and Special Numbers5:56
There's also less than and less than or equal to. So just switch the sign over. There's also equals, so there's double equals and triple equals, which we'll take a look at in the next video. And there's also not equals and not equals with two equal signs. So not is the !, and same for this one. So num1 is 4, num2 is 2. So these two should be true, and everything else should be false. So let's try that out. Actually, these two should be true as well, the bottom two, because num1 does indeed not
So let's try that out. Actually, these two should be true as well, the bottom two, because num1 does indeed not equal num2. Again, more on booleans in the next video. There's also special numbers, like NaN. So you get this when you try to perform some sort of operation that does not make sense. So let's console.log, say num1 times, so *, a string. So that doesn't make sense, so that should result in NaN. There's also Infinity. Infinity.
Type Conversion Basics8:49
So let's try 6.7, that will be 6. And there's other operations you can do as well, depending on what you need. Now let's take a look at converting types. Actually, I'm going to go up here and add two more variables that are strings. So say const myVar equals a string, and then another one for another string. So myVar2, say another string. Go back down here, let's say converting, and you already saw in the last video, if you use the plus operator on strings, then it's going to concatenate them. So if I do myVar, which is a string, plus myVar2, which is another string, then it will join those strings.
So if I do my var, which is a string, plus my var2, which is another string, then it will join those strings. Sorry, that should be plus. Okay, now what happens if you try to use the plus operator with one string and one number? So let's try that out. So this is a string, and we have num1, so plus num1, and num1 is 4. So what do you think we're going to get? So let's save that. And it's treating 4 as a string, and concatenates the two variables. So when you're using plus, and you have mixed types, it treats them as strings.
And it's treating 4 as a string, and concatenates the two variables. So when you're using +, and you have mixed types, it treats them as strings. Now what if you wanted to convert a string to a number? So let's try converting them. So string to number, let's console.log a string. So the string 2 plus 2, again, one is a string, one is a number. So the plus operator will be used as concatenation. So we should get 22 here. But if you want to convert the string to a number, or at least try to, then you can use the parseInt method.
But if you want to convert the string to a number, or at least try to, then you can use the parseInt method. So we can say parseInt, and then try to convert whatever is in brackets, in this case, the 2. And this should result in the number 2, and then plus should add the other 2 here. So there should be 4, and let's go. So if you look in DevTools here, the purple is a number, and the white is a string. How about the other way around? Convert a number to a string, so number to string. So again, this is console.log 2 here, that's a number, so it should be purple.
BigInt Limits and Usage12:10
Let's see what that number is. So it's this really large number here, and let's see what happens when we try to add one to it. So let's say maxInt++, like we saw above. Okay, so that seems to work, but let's try it again. So maxInt++ again. You can see it's not incrementing here. Actually, let me put another console.log here so you can see that properly. So I'll put it before we increment it. So that's the max value.
So I'll put it before we increment it. So that's the max value. We'll add one to it, and then we'll print it out again. So this one's correct, but this one is incorrect. It should be 3. So again, we get unexpected results here, so what we can do is make use of a bigInt instead. So to convert this to a bigInt, there are two ways of doing it. We can say bigInt in brackets, and now it should be a bigInt. Let's save this, and you can see it does work now, but there's this n at the end of the
We can say bigInt in brackets, and now it should be a bigInt. Let's save this, and you can see it does work now, but there's this n at the end of the number. So this n represents a bigInt, and you can see it is incrementing correctly here. So one, two, and three. So let me show you how to declare it using this n notation. So let me just copy this, and let's make a new variable down here. Let's name it bigNumber equals, and I'll just paste that in. And how about we change that variable? So bigNumber equals, how about we multiply it by another number, say four?
And how about we change that variable? So bigNumber equals, how about we multiply it by another number, say four? So times four. Let's try that out, and I have a typo here. And now it says you can't mix bigInt and other types, so this is a bigInt, but four is a normal number, so we have to use a bigInt here so we can add an n, and now that should work. And we have to console.log it, of course, bigNumber, and we do get the correct value for that big integer. So yeah, those are some operations you can do with numbers.
