Jonas: Starting from year's 2021.
We can use a feature called "Numeric Separators"
to format numbers in a way
that is easier for us,
or for other developers to read and to understand.
Let's say that we wanted to represent
a really large number.
For example, the diameter of our solar system.
Let's say a diameter,
which is 287,460,000,000.
And just from looking at this number here,
it's really difficult to read and to understand it.
Does this mean like 28 billion or is it just 2 billion
or really what is this number?
It's really hard to read like this.
There is just too many zeros here.
Now to help with this,
when we write a number this large
under normal English language,
we usually use a thousand separator like the comma.
We would write this number like this.
And then it becomes really easy
to immediately see that this means 287,460,000,000.
And now, fortunately,
we can do the same thing in JavaScript as well,
using the new numeric separators.
Numeric separators are simply underscores
that we can place anywhere that we want in
or numbers,
and which will make it really easy to understand
and to parse numbers this large.
Using the underscore here as a thousand separator,
then makes it really easy to understand,
that this number here
means in fact, 287 billion.
Now let's just log it to the console
to see what JavaScript actually sees.
And so here we see that in fact,
the engine basically ignores these underscores.
So these numeric separators.
It simply sees the number itself.
And so what this means,
is that we can actually place the underscores,
the numeric separators, anywhere that we want.
Let's do another example.
Let's call this one price in cents,
and I'm gonna set it to 345_99.
Again, using the underscore here,
then makes it really obvious
that this is actually a price in cents.
Of course the number itself, does again,
not contain the underscore.
And our price is indeed 35,599 cents.
But by placing the underscore here,
it makes it really obvious,
that we have first this value here
and then this other value here in cents.
And in fact, it's so obvious that we could even get rid
of the cents part here in a variable name.
Basically we can use the underscore now
to give meanings to certain parts of our numbers.
Here we said, these are thousands,
and here we said that these are a cents.
Lets try another one.
Lets call this transfer fee.
And here again, we placed the underscore like this.
And so here immediately,
it looks like it is $15 for example and 0 cents.
But if we did the same thing like this,
so placing to underscore here,
then it suddenly looks like 1_500.
While in fact it is the exact same number.
Both of them are exactly 1,500.
But just the underscore alone,
gives them different meanings.
And so we can use that to our advantage,
whenever we write numbers in or JavaScript code.
Another variable here, called PI.
And this one is to show you some restrictions
as to where we can place the underscore.
PI is,
3.1415,
and we can only place underscores between numbers.
We could place it here,
and there would be no problem at all.
Lets just log it,
but actually to underscore
doesn't make a lot of sense here.
Lets remove it and let's say we wanted to place it here.
Here we get to an error,
because that is not allowed.
It's also not allowed to place here,
and also not at the beginning of a number or at the end.
All of these are illegal,
and like this it works,
but we can also not place two in a row.
The error message here is pretty explicit about that.
And now just one final detail that we need to be aware of
is that when we try to convert strings,
that contain underscores,
to a number that will not work as expected.
Lets try to use the number of function
that we learned before to convert a number.
Of course with this, it should work just fine,
We have 230,000 to the number or actually the string
and then here it converts to a number.
However, if we place an underscore here,
so if we want to have a numeric separator here,
that would not work.
Now it is not a number.
What that means,
is that you should really only use,
these numeric separators,
when you are writing down numbers like this.
Really in the code.
If you need to store a number in a string,
for example, in an API,
or if you get a number as a string from an API,
you should not use underscores in there,
because then JavaScript will not be able
to parse the number correctly out of that string.
It's not gonna work as expected
and you will get it's not a number,
that might then introduce bugs into your application.
And the same is true with the "parseInt" function.
Here we do not get not a number,
but we only get to 230.
Only the parts that is here in front of the underscore.
Everything else will then be ignored.