JS Data Types - number vs BigInt questions
Posted by bocamj@reddit | learnprogramming | View on Reddit | 4 comments
Hi there, I'm learning data types in javascript. Messing around. I used these variables.
let x = 15;
let y = 123456789999;
typeof shows them both as numbers. So it got me thinking...
- Where does number end and bigint begin? I went as high as let y = 1234567899999999999999999999999999999; and it was still a number. When I put an n on the end, it's bigint, so
- What does n stand for or translate to? Is it infinity, or does it make it some continuous number? I thought number and bigint were separate DTs for memory purposes, so
- Is there an explicit way to declare a number vs bigint? I want to see what happens if I declare a bigint as a number and vice versa. But number is reserved, so I can't "let number = 123456789999n".
- Lastly, does anyone use bigint in programming, I mean, does it serve a practical purpose?
Thanks
jcunews1@reddit
JS' BigInt type is a string based number. So it has no standardized lower and upper limits. But it does have those limits depending on its implementation. e.g. both Firefox (SpiderMonkey JS engine) and Chromium (V8 JS engine), have different lower/upper limit in terms of number of digits from both below & above decimal point.
The
n
is just a type specifier for BigInt much like the"
/'
for String. i.e.123
is a number (the default),"123"
is a string, and123n
is a BigInt.BigInt is as it's design for. To handle very big number. This is expecially useful for exact scientific measurements. Both in small and large scale, where the number of digits is greater than 15 - which normal JS number type can't store or can't accurately store.
bocamj@reddit (OP)
Thanks
teraflop@reddit
It's not like values below some threshold are numbers, and above that threshold they're bigints. They're two entirely different ways of representing numeric values.
JavaScript's "number" type is a floating point type, which means that numbers are rounded to about 53 significant binary digits (which is roughly equivalent to 16 significant decimal digits). So you can store very large numbers, but your results will be inexact.
Try calculating
10000000000000001 - 10000000000000000
and see what you get.It's just syntax that tells the JS parser to interpret your numeric constant as a BigInt instead of a Number, so that it will be represented exactly.
No idea what you mean by this.
Yes, like I said, that's what the
n
suffix is for.The name of your variable is just a name. It doesn't have any connection to the data type that you store in the variable.
Well, any time you want to do exact integer arithmetic without rounding.
Just as one example, cryptographic algorithms often do arithmetic on integers with hundreds of digits. The exact value of those integers is crucial, and any rounding or inaccuracy would completely ruin the output.
bocamj@reddit (OP)
OK, thanks.
I got 0
And what I meant about by infinite is if that's what n means, but yeah, I guess it just is what it is. I thought there might be some mathematics behind it, like putting an n on the end, literally translates to an infinite number of 9s on the end, or more like PI. An infinite decimal representation. I guess that's what I meant. Just trying to figure it out. And now I'll move on ;)
On the other part that didn't make sense to you, I suppose I was thinking maybe in the declaration, if the number isn't bigint, how could I make it bigint without an n on the end, a physical number. The only thing I could find is if I do something like const bigIntValue2 = BigInt(12345678901234567890);
And honestly, that's not exactly what I was asking. But you answered it for me. I suppose I was hoping there would be a number - if large enough (like Octillion) - that would be bigint, without needing an n on the end.