Professional JS: Features You Need to Know

BigInt & Number Methods

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Professional JS: Features You Need to Know

Check out a free preview of the full Professional JS: Features You Need to Know course

The "BigInt & Number Methods" Lesson is part of the full, Professional JS: Features You Need to Know course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano discusses the BigInt object in JavaScript, which allows for the representation of whole numbers larger than the maximum number that JavaScript can handle. He also mentions other methods and constants related to numbers, such as isFinite, isInteger, and MAX_SAFE_INTEGER.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "BigInt & Number Methods" Lesson

[00:00:00]
>> Maximiliano Firtman: Numbers, let's get into numbers. You know that numbers in JavaScript is always a problem, even if it's part of most of the JavaScript jokes. There are a lot of jokes around and memes around JavaScript, and most of them are because of how numbers work. You know that we don't have integers and floats, we have just number, and the precision of that is not really good because by default it's always a floating point number even if it's an integer and it has a limit and blah, blah, blah.

[00:00:34]
Well, now we have from 2020 BigInt. A built-in object that provides a way to represent whole numbers larger than BigInt long numbers is to power 2^53-1. That's actually the largest number that JavaScript can represent with a number. And you just write the number with an n suffix, lowercase n at the end.

[00:01:02]
And you can apply all the math operations with only 1, for example in this case I'm adding 10. Lets add 1 so we can probably see this better the ns in 1, okay? Instead of ending in 0, it's ending in 1, so it's adding 1. The thing is that we have a problem.

[00:01:21]
What if I just wanna add 1? If I run this, look at this, I cannot mix BigInts with numbers. So I have to use converters. I mean, if it's literal, I can use an n, okay. But let's say I have another variable that is coming from a JSON, from a server, and it's already a number.

[00:01:46]
Well, you can convert it into BigInt just by using this constructor.
>> Maximiliano Firtman: And also you can do the reverse operation in case the number is not so big, but you can convert this into a number. If you take a BigInt, you can convert it back into a number if it's within the limits of the number primitive, okay?

[00:02:11]
Makes sense? It's just that, again, you won't probably use this in a daily base also, but having minus there, and some other minor additions around numbers. So, new methods for example, is finit, is integer, is save integer that is actually been safe or not? It's kind of weird, but let's say quickly, that a safe number is a number that can be represented by a number without losing position.

[00:02:46]
It's kind of that, okay? So you can check if something is or not safe, if it's a none, if it's not a number, you have some new constants for the max safe integer. So here, have the max safe integer, integer at the minimum, safe integer, and after that, you're losing precision.

[00:03:05]
And now we also have a way to use separators just for seeing them nicer. So having those separators, it's an underscore or not, it's just the same. So you can have there wherever you want. So, typically we use it every 3D sheets, but you don't need to, okay?

[00:03:28]
It's just a character that will be ignored by the parser when it's within the number. Typically four thousand separators. It's just that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now