Skip to main content

Dealing with Long datatypes (i.e. 64-bit integers)

JavaScript does not natively support Longs

In most languages, a long is a 64-bit integer, for use when a 32-bit integer isn't big enough. JavaScript doesn't have such a concept: all numbers in JavaScript are represented as 64-bit doubles (i.e. double precision floats). That does not have the same maximum value as a 64-bit integer!

As a result, some "numbers" you might want to store in JavaScript - for example, a Twitter status ID - are too large to fit into the normal datatype and need to be used as strings most of the time. If you don't, JavaScript will forcibly truncate the integer as it cannot represent it precisely enough. This can cause all manner of seemingly unclear errors in applications, where you end up with invalid data with a bunch of zeroes on the end because JavaScript truncated it.

BigInts are a thing in JavaScript, but many applications that need to deal in large numbers use strings for legacy reasons.