Loading
Current section: 3. Spread and Rest 4 exercises
lesson

Intro to Spread and Rest

Loading lesson

Transcript

00:00 Alright. Let's talk about one of my favorite features in JavaScript and that is rest and spread operator. It's literally just a bunch of dots. That's all it is. It's so great.

00:09 So, let's talk about this first. Before I show you this one, let's look at, what if I wanted to make an array that was comprised of all of what is in array one plus four five? This is what I might do. Make a new array, make an expression for grabbing the first, second, and third items, and then put four five. That would be super annoying especially if I decided to add another one right here.

00:32 Well, now I gotta go over here and add that. Super annoying. Don't wanna do that. Instead, the idea is take all of these things and put it into this new array. And so that is what spread does is it takes all of the things here and spreads them into this array and effectively combining those two arrays.

00:51 Objects, basically the same thing. Take all the properties here, spread them in this position and then here's the rest of those things. And it has interesting implications when we're talking about what if there's a conflict in the property names and stuff. We'll talk about that. And then on the rest side of things, this allows you to, collect a bunch of values together.

01:13 So here we have this sum. We're gonna take these, all the numbers that are passed to it. So here's all the numbers that are passed to it and turn that into an array of numbers. The alternative would be to not use this syntax and pass it as an array, which may like it probably works just fine, but it's a little less ergonomic. And so it's nice to be able to just say take all these things, grab them all.

01:36 And, we're gonna cover all sorts of things around that too. It's gonna be great. And then we'll talk a little bit about how things aren't, totally immutable, with stuff too. It's it's kind of interesting, especially when you're talking about sub objects or objects that have properties that are themselves objects and we'll talk about all of that stuff too. It's going to be fun.

01:57 I think you'll love it. So why don't we just jump right in?