In this on a race, we're going to talk about the spread operator, which is something of a fairly recent
development in JavaScript, and it can cause some confusion because it's not immediately clear what
it does.
So let's take a look.
So what a sprint operator does is to create an array for misapprehend for the array spread operator.
So that's going to do is break apart the array into its individual components.
So if I say if I have it in array called Operand and the sprint operators, dot, dot, dot, I can
create a new array using dot, dot, dot operand because with operand does is breaks the array into
its individual elements.
And then because I put the square brackets around it, it puts it back into being a new array.
So this is a way to clone an array and I'll explain what that means in the second.
So we're going to Pandoras together using the same methodology, five three arrays.
I can say dot, dot, dot array, one comma, dot, dot, dot array two comma, dot, dot, dot, array
three.
And then anything you can iterate through, you can turn it into an array.
So, for example, if I have my string and I put dot, dot, dot in front of it, it's going to turn
it into a ray of Amcom, like I'm esq, et cetera.
And then an example from the math library is, if I want to find the minimum value in an array, I can
use the spread operator because what the minimum does is it takes a list of numbers separated by commas,
and so you can't pass an array to it.
So what you do is use the spread operator to break it into its individual elements.
So with all that said, and it's confusing, but hopefully we can clear it up a little bit by doing
some examples.
All right, let's start with an array.
And first, let me talk about what cloning is and why it's useful if I say let my array
equals two, four, six, eight, 10, and then I say let the other array equals my array.
We have made a copy of the array.
We are just using the same array with a different variable.
So if I say other array, so two equals 200 and then I logout my array,
we're going to see that other array changed my array.
So let's run that.
So we see other ouray now has the value of two hundred, so Meira also has that because when we did
this, all we did was a copy, the start of the raid, a pointer to the array.
So with the spread operator, does it lets us create a new array.
So let's do this again, except then use the spread operator.
And I say dot, dot, dot my array.
Now, before we get that far, what's the dot, dot, dot.
So I'm just gonna say cancel that log.
I thought that my oray and let's comment this other stuff out for right now.
So until you do this, it's hard to understand with the dot, dot, dot spread operator does, but all
it does is breaks it up into individual numbers.
So this is just like we have five variables and one variable holds two, one variable, four, et cetera.
All right, let's uncommented this.
So now other ready when we sign at the 200 mile race, still going to have two, four, six, eight,
10.
So let's put them both
and run it.
So other Arae has the 200, but my race has six.
So what we've done is made a copy of what's in the array, all right.
And this honestly just takes practice.
You just have to get used to using it and then it starts to make sense.
So let's do the concatenation now.
We have to raise so say let one more array equals dot, dot, dot, Myra, dot, dot, dot, other array.
Got that.
And we'll give it even one more round.
We'll put some strings in this one if you see
the F
and then we'll cancel all this out.
One more Arae, so let's see what that has.
So it's got all these first two arrays in one array plus as ABC and D.F. elements.
So what is concatenated?
Three arrays together?
Very simple.
And then the math thought men.
Let's take a look at that.
So if we have let's look at math men first.
So it gets us the minimum value of a set of values.
So what this allows you to do is send in a set of values to get a man so I can say to come forward,
come a six.
Come eight.
Come on one.
So what's up with that?
So our method then returns one, but if we have an array, it's a little bit more handy to be able to
use the spread operator if I just put an array in here.
We get not a number, which is what NASA stands for, because the array is not a number, it's an array.
So what we need to do is use the spread operator to break it up into its individual elements.
Now we get a list of numbers.
And so we get to examine.
All right, so that's the spread operator, and all I can say is you got to practice and get used to
it so that when a need comes up for it, you think, oh, that's a good use for the spread operator.
So we'll be talking about this more later and destruction is next, and that is sort of similar.