TOC

The community is working on translating this tutorial into Italian, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Data types:

Numbers

Working with numbers is quite important in any programming language. Internally, computers will treat different types of numbers (integers, floating point numbers etc.) in different ways, so a lot of programming languages will as you to do the same, but with JavaScript, you are free to mix and match different types of numbers.

Working with numbers

So for instance, creating a variable with an integer is as easy as defining it and assigning the number to it:

let n = 42;

And if you want a floating point number, no problem - you can do that just as easily. Simply use a period to separate the whole number and the decimal part, like this:

let n = 42.123;

This also means that you are free to do math with integers and floating point numbers as if they are the same type, which makes things a lot easier. Here's an example:

let n1 = 2, n2 = 40.123;
alert(n1 + n2);

Numeric separator

When defining large numbers, you may find it hard to see just how big the numbers is when reading the code:

let n1 = 42000000000;

JavaScript can help here, with a bit of syntactic sugar in the form of the numeric separator - you are free to add underscores to create groups of digits:

let n1 = 42_000_000_000;
alert(n1);

Converting strings to numbers

Sometimes you want to take something that is actually a text string and then convert it into a number. There are two globally available functions for that: parseInt() and parseFloat(). Their names make them quite self-explanatory - they will simply take a text string and, if possible, convert it into a number, assuming that the text string is in the form of either an integer or a floating point number.

let n1 = "40", n2 = "2.42";
alert(parseInt(n1) + parseFloat(n2));

If we didn't convert them into numbers first, JavaScript would think that we were trying to concatenate two strings, resulting in the value 402.42 instead of the desired result of 42.42.

As an alternative, you can use the Number() method - it will try to take any type of input and convert it to a number:

let n1 = "40", n2 = "2.42";
alert(Number(n1) + Number(n2));

Not a Number (NaN)

Even though JavaScript is very flexible when dealing with numbers, there will be situations where you want to make absolutely sure that the value you hold is, or could be, a number. JavaScript comes with a specific, globally available constant for this, called NaN (short for Not a Number). It will be returned by several JavaScript functions if you try to do something that is expected to return a numerical result but it's not possible. An example:

alert(parseInt("forty two"));

Here, the parseInt() function is expected to return a number, but "forty two" isn't really a valid number, so to communicate that this wasn't possible, NaN is returned instead. This also means that sometimes, you need to check for NaN, to make sure that the result of an operation was in fact a number. Here, you should use the globally available isNaN() function, which will simply tell you whether the passed value "is Not a Number":

let n1 = parseInt("forty two");
if(isNaN(n1))
	alert("Sorry, that's not a valid number!");
else
	alert("Cool number!");

BigInt

Normal numbers in JavaScript are some what limited in how big or small they can be. When dealing with integers, the regular number type has a minimum and a maximum value, which you can always check by checking the Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER constants, like this:

alert(Number.MIN_SAFE_INTEGER);
alert(Number.MAX_SAFE_INTEGER);

If you need to work with larger numbers than that, you can use the bigint type, supported by the BigInt object. You can specify a bigint type by calling the BigInt() method on it or by postfixing the number with an n, like this:

let bigNumber = 429007199254740991n;
alert(bigNumber);
alert(typeof bigNumber);

For most tasks though, you will likely be able to just use the number type - the bigint type is for really, really big numbers, as you can see.

Summary

Dealing with numbers is an important part of pretty much any programming task, and fortunately for us, JavaScript comes with lots of functionality to make things easy for us.

Behind the scenes, when declaring a number, like we did in many of the examples of this article, a Number object is created. On this object, you'll find even more number related functionality, and we'll discuss all of it in the chapter on built-in objects, specifically in the article on the Number object.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!