Check out a free preview of the full Introduction to JavaScript course
The "Numbers" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson:
Brian introduces the number primitive data type and gives a few things to watch out for when handling numbers.
Transcript from the "Numbers" Lesson
[00:00:00]
>> Brian Holt: So the last one we kind of talked about a little bit about but I just wanna go over it again. The last type, these are called primitive types, which means that they're irreducible. You cannot split them into further other things, right?. There's strings, there's booleans, and now we're gonna talk a lot again about numbers right?.
[00:00:18]
Number can't be split into something else, it's just a number, right?. So if you ever heard the term primitive type, that's what we're talking about. So the last primitive type that we're going to talk about is a number. So constant num equals five right constant num and like I was saying before.
[00:00:41]
There's no difference between a float and a float in another language, it would be like 5.001, right?
>> Brian Holt: In other languages they would split decimals out from integers, and that's not the case in JavaScript, you can also have this be negative.
>> Brian Holt: And all of that is just shoved into one data type, one primitive type which is called a number, unsurprisingly.
[00:01:12]
Now doing math with JavaScript is notoriously fickle,right? So if I try and do an integer addition, if I trans it to 5.00001. So if I trans it to num plus 1.123, let's see, sometimes you get these really crazy, they're very close, right? It'll be 000001, right? And it's not quite the same, but it's really close.
[00:01:49]
It's the nature of how JavaScript calculates its variables. So, in other words, don't rely on integer addition in math or things to actually truly be equal to each other. [LAUGH] In other words, you have to build it in tolerances. So, the reason why I bring this up is I used to work at Netflix, right?
[00:02:09]
I used to work on billing at Netflix. So if you're trying to do math with dollars and your math is not exact, kinda arises problem cuz you overcharge or undercharge people. Even if it's just by 0.0001, still technically a crime, so don't do that. [LAUGH] So be careful with math in JavaScript.
[00:02:29]
Yeah?
>> Speaker 2: Does JavaScript follow order of operations?
>> Brian Holt: Mm-hm.
>> Brian Holt: So just like you would normally expect, like if I do num plus one divided by two here, this is going to follow what you would normally expect from your math class. I'm not gonna teach a math class today.
[00:02:48]
[LAUGH] But this is gonna be executed first, right? Because division has a higher priority than num, or than addition, right?
>> Brian Holt: So, yes, it follows typical order of operations.
>> Speaker 3: Can you get overflow and underflow errors and issues in JavaScript?
>> Brian Holt: Yeah, the numbers are quite large.
>> Brian Holt: So gotta remember, math dot,
[00:03:17]
>> Brian Holt: Or, no, it's Number.MAX_SAFE_INTEGER, that's the biggest number you can have. It's usually enough for most people. We'll talk about what that is later, there's also admin's safe integer as well. Which is gonna be roughly the same thing in the negative direction. So if I try to do plus one, or rather minus one.
[00:03:47]
I'm not guaranteed what the results are gonna be. So in this case, it actually does handle it correctly. But that's what that safe means that if once you're beyond this point, good luck. [LAUGH] We wish you the best. Most of your math hopefully it falls within that realm though, right?
[00:04:04]
I don't know what that is. That would be
>> Brian Holt: Thousands, millions, billions, trillions, quadrillions, something that.
>> Speaker 3: It's in my Netflix bill.
>> Brian Holt: [LAUGH] You are welcome. Just kidding. So that's it for primitive types, right? That's the most basic building blocks. There are some other kind of fancier things, but you could always write JavaScript and never run into him.
[00:04:35]
So, for now this is gonna be what you're gonna be using 99% of the time.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops