If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
Modern JavaScript includes a built-in primitive type called big in which SS n a suggests can be used to represent arbitrary large intes. We can create a big end by writing a number as you normally put, and adding the suffix N to specify that this is a not just a number but instead it is a big int. It is a primitive type within JavaScript and the type of operator does work on it and it gives us back the string big int as the name also implies fractions cannot be a part of intes and big ins can only be intes. However, you can have a negative or positive big ints.
00:35
And the key differentiator between a big int and a number is that a big int can be as big as you want. As an example, if you try to calculate 10 to the power of 500 with just JavaScript numbers, that value would be too big for a number and in fact it gives us that special value infinity that we looked at in our number lesson. However, no integer is too big for a big int, so 10 to the power 500 as big ins will happily calculate 1 0 0 0 as many as you need with numbers. It is actually not that hard to run out of the safe integer space. For example, this particular number as a number data type would actually translate poorly
01:13
and you'll actually see 1 0 0 0 instead of 9, 9, 9, 9. However, the same value as a big int will be perfectly fine, as you mentioned, as big as you want. JavaScript also provides a big into construc, which can be used to convert as an example, strings of arbitrary inte size into big ints. Essentially passing the string to the big end constructor is parsing, and of course equality works with begins as well. This par value is exactly equal to a hundred N and this parsing logic also works with other number systems that we looked at in our number tutorial.
01:48
So the Hector Decimal 64 does still pass to a hundred and in decimal terms you can convert between a big end and a number, and safeguards exist to prevent silly mistakes, but even then you should really consider moving between the two. As an example, we can convert a big end into a number by passing the big end to the number constructor. And as you can see, a hundred and converted to a number is equal to a hundred in JavaScript. Similarly, we have already seen that we can convert a number into a big end by passing it to the big end constructor.
02:20
So the big end version of a hundred is equal to a hundred N. However, not all numbers can be converted to a big end. For example, if we have a number with a fractional part and we try to convert it into a big end, we will get an error in JavaScript and the error message will be that it cannot be converted to a big end because it is not an inte. Similarly, if you have a very large big end that cannot be represented as a number, then we will get back in infinity. And in the worst case, if it is still within a reasonable limit, we might even get the value wrong. For anything that is beyond max safe integer,
02:53
you really shouldn't be converting a big end into a number and for the same reason of possible loss of Precision, JavaScript doesn't allow you to mix numbers and big ins in standard mathematical operators. For example, in this particular case, it's going to throw an error that you cannot mix big ins and other types. You should instead use explicit conversions. And the intent here is that if you explicitly convert using the number constructor or the big in constructor, you are sort of accepting the risk that you might lose some precision. So if you wrap the 20 N into a number constructor, we can still do math between a number and a number big in supports all the
03:28
standard mathematical operators that you are familiar with for numbers, but with a slight caveat that fractions are obviously going to get ignored. We know from our lesson on numbers that any math beyond the max safe integer is not okay to do with the built-in number data type. However, with the big in, it's completely fine to do as big a math as you want. In addition to the addition operator, feel free to use other operators as well. For example, the multiplication star, the subtraction minus the power star, star as well as the remainder person sign. However, the one operator that you need to be cautious with is of course the division
04:02
operator. Because begins don't represent fractions, all of the fractional part will actually get completely ignored in the output begins support the standard comparison operators. And if you're only comparing begins, then it's perfectly safe. But if you throw numbers into the mix, it's a different story you can compare begins with other begins using the standard strict equality. You can check if one begin is less than the other or if it is greater than the other, or even if it is greater than or equal to or less than or equal to, and they all work perfectly fine. However, you should be careful when comparing big ins and numbers.
04:37
You cannot use strict equality as the strict equality cares about the types being the same. However you can use those equality, but that doesn't mean that it is a good idea and the other comparison operators seem to work reliably as well. However, be won. Since not all intes can be represented using the number data type, you might result in values that should be equal yet they are not. The guidance here is that pick a data type that is more appropriate for your use case and then run with it. Don't mix begins and numbers unless you are really sure that the values they contain are safe.
05:11
Biggin is another one of those JavaScript built-ins that you will most likely never use directly, but having a built-in primitive integer that can be as large as you want is important for JavaScript. It allows libraries to use Biggins internally to do big competitions and even provide nice reliable wrappers for mathematical things like big decimals. As always, thank you for joining me and I will see you in the next one.