
The spread operator is awesome. It can do SO much — it’s efficient, readable, and can save a whole load of mess. I don’t know how you can not love it!
It’s a reasonably new feature of JavaScript that came with ECMA2015 (or ES6, if you prefer, although the former is technically correct!) — or for those unfamiliar with these terms — a recent standard update of JavaScript.
So, What is the Spread Operator?
Three dots: …
Three very powerful dots, that allow you to expand arrays or iterable objects in places where array elements or key-value pairs would be expected.
There’s three such places: functions calls, elements in arrays, and key-value pairs in objects.
Let’s breathe a bit of life into those words and have a look at what this thing can do!
The Spread Operator in Arrays
Let’s say we’ve got an array letters which we want to insert into a new array:
We can just…

When you see the spread operator in an array, it’s just expanding one array into another.
Manipulating Arrays with the Spread Operator
There’s a few things you can do with spread syntax to manipulate arrays. I’ll include a couple of examples to show you what it can do!
Adding values to arrays

Self explanatory – and beautiful. Using the spread operator allows you to turn smallNums into a flat array (rather than a nested array, which would be the result without spread syntax).
Concatenating arrays
As an alternative to .concat(), you can:

Be warned though, if performance is important to you, this may not be the best choice as it actually runs a fair bit slower than .concat() — but it is still great to be aware of it because it’s really readable and tidy!
Another way to split strings
I don’t have anything against String.prototype.split() — but the spread operator’s way of doing it is pretty cool, and the kind of thing a nifty JS developer should know! Check this out:

Copying arrays
If you’re anything like me, you shallowly clone arrays all the time. Do you do it like this?

Yeah, so did I for a while! But then I discovered that you can actually just use the spread operator to make life easier…

This is absolutely great for complex applications where you’re managing immutable state, such as Redux or Vuex.
Function Calls
Let’s say we have a function which takes a bunch of strings as arguments and concatenates them:

Usually you’d need to pass in the arguments one by one, like this:

Using the spread operator, you can tidy this invocation up nicely:

The Future: Spread Syntax and Object Literals
Spread syntax for objects is a Stage 3 proposal for ECMAscript (at the time of writing).
Copying objects
Just as with shallowly cloning arrays, I also shallowly clone objects regularly. Until recently, the go-to method was Object.assign():

I’ve always liked Object.assign()well enough, but the spread operator does the job with far more grace:

Combining objects
Merging objects also becomes neater with the spread operator, an operation traditionally performed with Object.assign().

Compare with the spread syntax way of doing things:

This is all pretty similar to cloning and merging with arrays, but it’s fabulous that we’ll be able to do this with objects too!
Don’t Get Confused!
Beware — if you see … in code, it’s not necessarily the spread operator. It could also be the rest parameters.
Rest parameters create functions which can accept any number of arguments. You’ll be able to recognise them easily because they are always found within (and at the end of) function parameters. Here’s an example, just so you can recognise them:

I’ll do another blog post on Rest Parameters — watch this space!
Is your goal to become a pro developer? Become a Software Developer in as little as 13 weeks on the Northcoders Coding Bootcamp! Find out more at www.northcoders.com.
Get all my latest content on Twitter at @RuthYMNg
Keep coding! 🚀
JavaScript: The Spread Operator
The spread operator is awesome. It can do SO much — it’s efficient, readable, and can save a whole load of mess. I don’t know how you can not love it! It’s a reasonably new feature of JavaScript that came with ECMA2015 (or ES6, if you prefer, although the former is technically correct!) — or for those unfamiliar with…
Taking your JavaScript Skills to a Higher (Order) Level – Part 1
Take your JavaScript knowledge to another level, a ‘higher-order’. I remember reading about Higher Order Functions, watching videos and still not being 100% sure about what they are and what I should understand about them. Maybe it was a case of trying to run before I could walk but it all seemed so mysterious. This…
A Beginner’s Guide to Test-Driven Development
Test Driven Development (TDD) is an industry best practice, and it’s a method we teach our students from week 1 of the course. Throughout this post, I will be using Mocha as a testing framework, and Chai as an assertion library. If you’re new to programming, you’ll need to ensure you have Node installed on your machine before you read up…
Is Coding for Me? Signs You Should Learn to Code!
Ever considered learning to code? If you’re bored at work, consider yourself creative, or love learning, this post is for you! You like learning I’ve always enjoyed learning, even when I didn’t necessarily enjoy my schooling. I liked the feeling of approaching a brand new topic, having no idea where to start, and slowly feeling my…
Writing the Perfect Junior Developer CV
Poor CV? No interview. Great CV? Fast lane to interview. Your CV is your first chance to market yourself, and in many cases, your only chance to make a first impression. Make it count. In this post, I explain how to write a great Junior Developer CV What are the best practices? 1. Keep it…
What is Pair Programming and Why Do Developers Do It?
New to the world of programming? You might not be familiar with the term pair programming – yet. But one thing’s for sure, you will be by the end of this post! What is pair programming? Pair programming is a common software development technique where two developers work on the same code, together, at the…
Unlock Yourself
I’m overdue a post on coding and why, for me, it’s one of the most amazing things you can spend your time doing. I just wanted to get this one out first though. This is quite a personal post but I think it’ll resonate with at least a few people who read it. For me,…