…spread operator and rest operator – Beau teaches JavaScript



The spread operator (…) spreads out the elements of an array (or iterable object). The rest operator condenses elements. The spread and rest operators are great additions to ES6!

🔗 Code: http://codepen.io/beaucarnes/pen/EWabjW?editors=0012
🔗 More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator

Beau Carnes on Twitter: https://twitter.com/carnesbeau

⭐JavaScript Playlists⭐
▶JavaScript Basics: https://www.youtube.com/playlist?list=PLWKjhJtqVAbk2qRZtWSzCIN38JC_NdhW5
▶Data Structures and Algorithms: https://www.youtube.com/playlist?list=PLWKjhJtqVAbkso-IbgiiP48n-O-JQA9PJ
▶Design Patterns: https://www.youtube.com/playlist?list=PLWKjhJtqVAbnZtkAI3BqcYxKnfWn_C704
▶ES6: https://www.youtube.com/playlist?list=PLWKjhJtqVAbljtmmeS0c-CEl2LdE-eR_F
▶Clean Code: https://www.youtube.com/playlist?list=PLWKjhJtqVAbkK24EaPurzMq0-kw5U9pJh


We’re busy people who learn to code, then practice by building projects for nonprofits. Learn Full-stack JavaScript, build a portfolio, and get great references with our open source community.

Join our community at https://freecodecamp.com
Read great tech articles at https://medium.freecodecamp.com

source

This Post Has 37 Comments

  1. Ahmed Raafat

    Don't forget to subscribe and remember, USE YOUR CODE FOR GOOD!
    Thanks for the great explanation.

  2. Flying Potato

    Am i the only one who hard to grasp ES6 ? like my brain cells is not functioning lol.

  3. Yvng

    am confused at the last when i mutiply the parameters like this it outputs 'null', does anyone know why? for i.e:
    function multiply(multiplier, …theArgs) {
    return multiplier * theArgs;
    }
    var arr = multiply(2, 1, 2, 3);
    console.log(arr)

  4. Ace Borja

    Thank you. Very useful explanation. Great job Beau!

  5. Alex Labanino

    They should add this video to FreeCodeCamp because they have watered down the explanation on "Use the Rest Parameter with Function Parameters".

  6. P-A Morel

    I just found the series "Beau teaches JavaScript," and boy, this is the best JavaScript tutorial series (at least about the topics covered). He is straight to the point, has a clear and clean voice that is easy to understand, and uses a good speech pace. Well, anyway, You got a new sub with notifications on 😉

  7. David Gillis

    I don't understand the statment at 5:18 where you explain how you know the difference between the spred opperator and the rest opperator. They look the same to me and seem to be used the same. Perhaps I do not undersatnd what is means to be "passed into an argument as a function." Could someone elaborate?

  8. Amine

    Thanks for the clear and precise explanation of the 3 dots !

  9. Taylor Wade

    is there a benefit to using the spread operator when assigning arr2 to the value of arr? I tried coding it as let arr2 = arr; and it worked just fine. And the push method worked the same as well. I also tried concatenating by coding let arr1 = arr1 + ',' + arr2; which worked fined and that seems like less to type out. Is there a benefit to using the spread operator here as well?

  10. Weird stuff

    I am on my 3rd year of IT degree . I got to know this today

  11. AM Wostan

    Thanks Beau. I was stuck on the FCC lesson about REST parameters and this really explained it well.

  12. Javed Baloch

    To convert string to array

    let str = "Hello";
    let x = […str];

  13. J T.

    great explanation thank you! although the last one about the rest operator is still confusing

  14. Filip Pandrc

    I remember being here, watching this video, doing some exercises. But here I am again, not having a slight idea what spread and rest operators are. fml

  15. Alex Lions

    thanks )) you are one of the best teaacher in js in the world

  16. alfons tabz

    hmmm.. i almost get it don't understand line 33 this is the first time i saw two return statement in a function..???

  17. herolivesnu

    Wow! You did a great job here in your explanations. I can't just pass without saying "God will continue to bless you with more knowledge".

Leave a Reply