Spread Operator

BY far one my favorite methods in JavaScript has been using the spread operator. The spread operator allows iteration on array expressions or strings to be expanded upon. Great use case for this is lets say have a list of scores, and you simply want to return those scores, you can use the spread operator…

Now lets look at another example, lets say we simply want to return a String into a list.

The issue here is pretty obvious, the return statement is giving us back an individual letter instead of the actual string of letters. This make it seem like the spread operator seems not up to the task here, however if we simply add a couple brackets we can correct this issue…

The brackets make it so that the string is returned in its entirety, instead of of no brackets which make it so each individual letter is returned.

Another great use case for the spread operator would be using with the method push(), this adds an element to the end of an array and will return the new length of the array. Using this with at the end of the spread operator we can combined contents of two array together.

These are just a few examples of how we can use the spread operator to help make our coding easier, and provides benefit in real world practice.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gabriel Castro

Full Stack, Software Engineer. Focus in Rails, JavaScript and React.