rest operator | ...
Hey, I've looked into the rest operator and came to my own conclusion that it handles a group of things allowing them to be placed somewhere else. I've mainly seen it being useful in situations regarding objects and lists e.g.
I can't help but think that something else i've seen does something similar if not the same thing since i've never come across an operator like this before, i just can't put my finger on it. I'm pretty sure you can add two lists for example using
+
or something like that so i'm not entirely sure why there would be a need for an operator that let's you do this? what does the rest operator really do? Is there a special functionality i'm missing?
Thanks in advance.28 Replies
you can add 2 arrays in php, but not in javascript
and i don't know what the "rest operator" is
it's called the spread operator
the spread basically pulls off the first layer of what comes after
and Epic is right, you can't do
['a','b'] + ['c']
in JSoh, the 3 dots?
yes, that's the spread operator
react people LOVE it
it's got plenty of uses
but yeah, you see it a lot when people need to trigger reactivity in a lot of frameworks, because appending to an existing array isn't detectable for whatever does the reactivity
so vue and svelt and all the rest use the spread operator a ton too, afaik
it's useful to extend objects too
pojos, in this case
Both the spread and rest operators use three dots. The spread operator separates an object/array into a comma separated list, and the rest operator joins a comma separated list into an array.
The reason that the spread operator can be used to copy and/or concatenate arrays, is because it separates the elements into a comma separated list, which is how normal array items are placed into arrays. As a consequence. The spreading of numberList is the same as doing 1,2,3. Doing that inside the definition of concatList would be [1,2,3,4,5,6]. A similar thing happens when spreading john.
An example of using the rest operator would be in destructuring.
I did not realize they had different names, when used at the start or end of the list
i always just assumed it was called "destructive spread"
because that's inside a destructive assignment
oh, derp, I'm not even thinking straight, I didn't notice it was on the left side of the assignment in ChooKing's example
This message is all spread operator
Im still quite confused on the difference between the rest and spread operators. Does the name differ depending on the context?
no, it's two different operators that just happen to be the same symbols
the spread operator takes an array or object and then removes the outer layer
the rest operator goes in a list assignment or a function declaration and takes the "rest" of the items
rest - takes the rest of the values/arguments
spread - spreads the values on an array/pojo
that's what i got from my research
so if you have a function
add
:
You may want a version to add two, or three, or four, which you can do with the spread operator
You can call add_rest
with any number of parameters:
ah okay, so to make sure i understand, is this all correct?
rest = represents values/arguments. And an example of that would be the example jochem gave with arguments and the example chooking gave with the values?
spread = spreads out the values of an array/object, turning something like
let list = [1, 2, 3, "string"]
into 1, 2, 3, "string"
which can then be placed into other objects/arrays like the example in my original question
sorry epic, i didn't realise i had the reply on for youthat's okay
and sounds like you're in the ballpark of it
Awesome, thank you everyone for the help, it’s been super useful
so i researched it a little more, and have a question
what makes the first example a rest operator and the second a spread operator? the both seem to pretty much be doing the same thing which is imputing values? and since rest is for values/arguments the both seem like they should be rest to me?
this also confuses me a little more since going by jochems example the first example would be a spread not a rest however in the resource i read (https://www.freecodecamp.org/news/javascript-rest-vs-spread-operators/) it's a rest not a spread?
freeCodeCamp.org
JavaScript Rest vs Spread Operator – What’s the Difference?
JavaScript uses three dots (...) for both the rest and spread operators. But these two operators are not the same. The main difference between rest and spread is that the rest operator puts the rest of some specific user-supplied values into a JavaSc...
They are not doing the same thing. They are doing the opposite thing. The rest operator is creating an array from a comma separated list. The spread operator is creating a comma separated list from and array.
Ohh okay i see now. With the rest operator example, is the array you’re referring to the one returned by the return keyword or the set of parameters?
The others becomes an array because the rest operator is used on the comma separate list of 3,4,5,6,7. It doesn't apply to 1,2 because there are two other parameters preceding it which take those values. Without the dots, the value of others would be 3.
I am not sure if I understand the question, because the value returned is a parameter.
I was just confused what you meant by array in that situation. Does the rest operator return values in a comma separated list as an array?
The rest operator creates an array from whatever comma separated values it receives. This is not in any way related to the fact that the function returns something. That function just happens to return the variable that a rest operator was used on, but the return itself has no connection to the fact that it's a rest operator. You can use a rest operator without ever returning any part of it.
I used the wrong phrasing I think, I’m aware it’s not related to the function returning anything, for example, the array method
.filter
always returns an array with its results, I was wondering if the rest operator did the same, and as you say, I think it does?basically, the rest operator takes the rest of the arguments in a function
literally that
if you have 2 arguments and then a
...numbers
, then the variable numbers
will have all the other arguments
from jochem's example, calling add(1, 2, 3, 4, 5, 6)
if the function is declared as add(a, b, ...rest)
, then a = 1
, b = 2
and rest = [3, 4, 5, 6]
it doesn't create any "comma separated" anything
it's an array with what wasn't picked by the other arguments
the excess ... the rest
the spread operator inserts the elements of the variable into the position where the operator is used
for example:
it's also not a "comma-sepated" something
when someone says "comma-separated", it's a string
something like this: 'a, comma, separated, string'
Oh right I see now, sorry to make everyone explain that again, I appreciate it. Thank you.
Oh, when chooking mentioned a comma separated list I thought he was referencing the arguments when the function was being called e.g.
example(1, 2, 3, 4, 5, 6, 7)
the correct term for that is "arguments"
"parameters" is acceptable too, but i don't consider it correct
Ah okay, got it
Thanks again
you're welcome
also, a list is just an array
an array without any sparse or missing elements
just 0 to the length-1, with values