Learning JavaScript, Day 14 – Numbers
Today, along with learning about how JavaScript uses numbers, I am also learning how to eat a Kinder Joy, because, well, dinosaurs.
I do like how I don't have to eat everything just to get at the prize. Because I want to play with my sinoceratops and dilophosaurus right now, dangit.
Well, no. first I do JavaScript. Then I play with dinosaurs.
JavaScript Numbers
I do like that JavaScript has only one type of number and you don't have to differentiate between those with decimals and those without.
The scientific notation ones will get me, but that's because I never really immediately get those anyway. Like, sure, in Animal Crossing: New Horizons, I might have 785e5 bells, but 78,500,000 just looks better, doesn't it?
But since it then shows me that integers are only accurate up to 15 digits, I suppose the scientific notation needs to come in. I mean, as much as I would love to have 7,000,000,000,000,000 bells, I also don't want my JavaScript maths to be inaccurate, so 7e15 will have to do.
It then shows me that floating point arithmetic isn't always accurate by having an example.
let x = 0.2 + 0.1;
Which comes back as 0.30000000000000004.
What? Why? Why are you like this? What are you doing?
So they say "Well, use multiplication and division in it".
let y = (0.2*10 + 0.1*10) / 10;
Which seems soooooo complicated what the helllll.
But, okay, fine. I'm writing it down so that if it comes up when I'm working on something, I will remember that I did come across it. I don't understand, but I am accepting.
Adding Numbers and Strings
They then go through addition, which is really just about remembering that strings are concatenated and numbers are added and both use +.
So you can have
let existingBells = 785e5;
let pocketBells = 1e5;
let account = existingBells + pocketBells;
document.getElementById("demo").innerHTML =
"Your account is now " + account;
And get the right amount (Your account is now 78600000).
But if you
let existingBells = 785e5;
let pocketBells = 1e5;
let account = existingBells + pocketBells;
document.getElementById("demo").innerHTML =
"Your account is now " + existingBells + pocketBells;
You'll get Your account is now 78500000100000 because you started with a string and by god, everything is a string now.
They then show that if you have numbers as strings, you can use every other numeric operation in order to do maths with your strings.
It's just because + is concatenate along with add that makes it annoying.
NaN
You can't do maths with a non-numeric string – you get NaN in response.
let existingBells = "78500000";
let pocketBells = "100,000";
let account = existingBells - pocketBells;
document.getElementById("demo").innerHTML =
"Your account is now " + account;
Because I put the comma in pocketBells, I get Your account is now NaN. Oh no, I have broken my bank account, I have no money, how will I pay off Tom Nook.
There's also isNaN(), which will return true or false depending on whether or not the result is not a number.
So if I do isNaN(account); on the above, it's a true. Which is a check I can do, and then return a statement that says something like "I do not understand your input, please try again", and I can go "oh, dumbass, you stuck a comma in again."
Infinity
If you try to divide by 0, JavaScript doesn't throw an error. It just says Infinity, which is kinda neat. It just throws up its hands and goes "Too much number".
I also like their other example.
<script>
let myNumber = 2;
let txt = "";
while (myNumber != Infinity) {
myNumber = myNumber * myNumber;
}
TOO MUCH NUMBER CANNOT COPE.
It then gets into Hexadecimal numbers, switching bases, and numbers as objects, but this is all a bit too theoretical and deep math for me. I'm not skipping it, but I'm sort of...skimming. Yeah.
Day 13 — Results
- JavaScript doesn't care if you have decimals in your numbers or not.
- You can also use scientific notation, like
785e5for78500000. - You can only go up to 15 digits with numbers.
- Sometimes it has problems with floating point arithmetic.
+is both addition and concatenation.- This means that while you think you're adding things, you're actually concatenating strings if you have strings in there.
- If your strings are numbers, it will try to do maths with them. Just not with
+because concatenation. - If your string is not a number, trying to do maths with it will result in
NaN- Not a Number. isNaN()will check whether or not you're trying to bring inNaN.Infinityis a number.- If you divide by
0, you will getInfinity. - If your number gets too big, you get
Infinityinstead. - It can read hexadecimal numbers.
- You can switch bases if you want.
- Both of those make my head hurt.
I'm only stopping now because the next part is Number Methods, which is going to just be like String Methods and that's going to be a very long post, so I don't want to make something too long today.
Today's Sticker

Oh no, someone's washed up on my keyboard. We better wake him up and make sure he has a way to get home.
I think this was part of a collection I got from Wish ages ago. I can't remember, but I know I ended up using most of them on water bottles and the like. And gave away a bunch at EMF two years ago.
Thankfully, there's another EMF this year, so my now overflowing sticker box can get a bit of a clear out. If you're going, look out for stickers!