DEV Community

Cover image for ES6 - Spread Operator

ES6 - Spread Operator

skaytech on July 30, 2020

Introduction In this article, let us look at a very powerful yet simple feature introduced with ES6 or ES2015 version of JavaScript, the...
Collapse
 
pengeszikra profile image
Peter Vivo • Edited

Object spreading, also helpful at react reducer function:

const fooReducer = (state, {type, ...payloads} ) => {
  switch(type) {
    case 'set-foo': return {...state, foo: payloads.foo }; 
    case 'set-other': return {...state, other:payloads.other };
    case 'toggle-switch': return {...state, switch: !state.switch };
    default: return state;
  }
}
Collapse
 
skaytech profile image
skaytech

Thanks for sharing!

Collapse
 
hemant profile image
Hemant Joshi • Edited

Hii Skay,
Thanks for writing such master peice posts😁, helped a lot in my case
πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰

Collapse
 
skaytech profile image
skaytech

I'm glad you found it useful. Please share it to reach maximum number of people.

Collapse
 
hemant profile image
Hemant Joshi • Edited

Sure😁,

Also sorry in top comment I wanted to add Hii and autocorrect made it Byy😰

Thread Thread
 
skaytech profile image
skaytech

No worries :-) I understood you were saying Hi.. have a great weekend and keep learning...

Thread Thread
 
hemant profile image
Hemant Joshi

πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰

Thread Thread
 
hemant profile image
Hemant Joshi

Also wanted you to request to make another post on .map in Es6😁,
That would be great.

Thread Thread
 
skaytech profile image
skaytech

Sure. I'll make one not just for map. But, higher order array functions such as map, filter, foreach, etc. Thanks for the suggestion.

Thread Thread
 
skaytech profile image
skaytech
Thread Thread
 
hemant profile image
Hemant Joshi

Thanks

Collapse
 
bharatmhaskar profile image
Bharat Mhaskar

I think you should also cover "The optional chaining operator (?.)" as spread operator is used to accessing deeper values in objects with safe defaults.

Collapse
 
skaytech profile image
skaytech

Could you provide an example of how it's related to the spread operator?. Or is it used along with spread operator? I'm not quiet sure how to relate that with the spread operator.

Collapse
 
bharatmhaskar profile image
Bharat Mhaskar • Edited

Example with optional chaining operator

let customer = {
name: "Carl",
details: {
age: 82,
location: "Paradise Falls" // detailed address is unknown

}
};
let customerCity = customer.details?.address?.city;

With spread operator
let { details: { address: {city: customerCity } ={}} = {} } = customer || {}

Collapse
 
easrng profile image
easrng

I also use it a lot to turn things like the output of document.querySelectorAll into an array.

Collapse
 
skaytech profile image
skaytech

Totally! I find that as probably the most common use cases amongst all. Glad you took the time to read through the article. Thank you!

Collapse
 
dr_coderr profile image
ASHISH

Nice article... Detailed explanation, and exercises are really helpful.

Collapse
 
andrewbaisden profile image
Andrew Baisden

Good article thanks for sharing.

Collapse
 
skaytech profile image
skaytech

I'm glad you enjoyed the article! Thanks for reading!!