Check out a free preview of the full Introduction to JavaScript course:
The "Math & substr" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson's course:

Brian introduces additional commonly used math and string manipulation builtins.

Get Unlimited Access Now

Transcript from the "Math & substr" Lesson

>> Brian Holt: So another one is like we were talking about how numbers are kind of weird in JavaScript. There's a math collection of functions that you can call. So this one I call console.log(Math.round(5.1). This works exactly like how you would expect it to, it rounds to the nearest number.

So you can see here it gives me five. You can also say round down and you can force it to round down. So if I say floor, it always rounds down, even if this is like 5.9 right? And same with ceil. Ceiling, then someone got lazy and didn't want the I-N-G, so.

Even if this is 5.1, it rounds up, right? There’s a bunch of them, like the math object has a lot of stuff. If I put max and I give it two things, it’ll return to me the bigger one. That can be useful, right? And min as well, right?

So it’ll return to, and I can give it as many numbers as I want.
>> Brian Holt: Right? It'll just say like, okay, I'm just gonna give you the smallest one of all those numbers that you gave me. So, again, these are on MDN, don't memorize these. That's kind of a rule of thumb.

If you know how to Google it quickly, you don't need to memorize it. And also, please Google everything. Little known fact, developers are just professional Googlers. I'm constantly looking this stuff up, constantly. So there's no need, there's no expectations that you need to memorize any of this, right?

Mostly you just need to be familiar enough with it that you can Google it later. Okay, here's another one. So I put my name here, and there's a thing called sub-string or sub stir. And the first number is, now we've got to remember. It's where to start, right, so if you go through Brian Holt, the sixth position, which starting from 0, 1, 2, 3, 4, 5, 6, it's how it starts there, right?

Again, remember, starting from 0, because everything starts from 0, and then 3 is how many characters that you want to take, right? So notice it's returning Hol, so it takes, it starts at 6, it goes one, two, three, and then it stops. So that's how we get that.

So what happens if I change this from 6 to 4, it's gonna give me back the n H. If I get rid of the three, it's going to take all the way to the end of the string.
>> Speaker 2: Can you add a third number where, you know, it tells you where to start?

And then the second would be like, step. So you want every second letter from there.
>> Brian Holt: I don't know. So let's look it up.
>> Speaker 2: Okay.
>> Brian Holt: So I mean, this is exactly my point, like, I've been doing this for over a decade at this point. I don't know.

[LAUGH] I have to look this up almost every time that I do this. So I would just come in here and say substr. It's confusing, because there's also a sub string. And they're two different things and I can't remember why they're different. I just see like, you can see the first one is sub string.

And the second one is substr, so.
>> Brian Holt: You can even see here that it's not strictly deprecated, whatever, I don't care. So, if you go down here, this is the end, so the first number is start, the second number, which, if it's in these square brackets, means it's optional, right, the length.

So that's when we're going to use that one, you might be talking about the other one else. Let's take a look at what that one is.
>> Brian Holt: Index start and index end. Okay, so this one, you would say like, from five to seven and it would give you a three length one, right?

Whereas the substr gives you, you give it a length. So they work roughly the same way. So you'd have to write your own function to do something like that. Which, now that's your assignment.
>> Speaker 2: [LAUGH]
>> Brian Holt: Cool, so yeah, again, there are tons and tons and tons of these.

>> Speaker 2: And make use of them, right?
>> Brian Holt: Someone else, if it already exists inside of like the browser and all that kind of stuff, then just use it, right? Because that means someone else wrote it, it's already fast, it's already tested. So yeah, you should definitely lean on those kind of things.

You should just learn more and more about them.