DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Randy Rivera
Randy Rivera

Posted on

Combining Arrays with the Spread Operator

  • Another huge advantage of the spread operator is the ability to combine arrays, or to insert all the elements of one array into another, at any index. Spread syntax makes the following operation extremely simple.
  • Let's take this for example. I have defined a function spreadOut that returns the variable sentence. I modified the function using the spread operator so that it returns the array ['learning', 'to', 'code', 'is', 'fun'].
function spreadOut() {
  let fragment = ['to', 'code'];
  let sentence = ['learning', ...fragment, 'is', 'fun']; <----
  return sentence;
}

console.log(spreadOut()); will display  
['learning', 'to', 'code', 'is', 'fun']
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Classic DEV Post from 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!