DEV Community

Cover image for Three dots "..." in JS - Spread and Rest explained

Posted on


Three dots "..." in JS - Spread and Rest explained

Three dots ... is one of the favorites for JavaScript developers as it has a lot of use cases which makes their life easier.

Well, you can see this operator in JS concepts - spread, rest. Let me make one thing clear before we begin, the syntax is the same in both but use-cases are different.

As the syntax is the same it might be very confusing, especially in the interviews (as you might be tensed) when the interviewer tries to trick you on this. Believe me, I have made mistakes on this in my first JS interview 😅.


The spread operator will be used to split up array elements or object properties (hence, the name spread, i.e. “spread the elements of an array/objects in a new array/objects”). Let's just understand what I just said.

const newArray = [...oldArray, 1, 2]
const newObject = [...oldObject, newProp : 'a']

You can quickly check the examples below to understand it better

- arrays

const numbers = [1, 2, 3, 4]

const numbers_with_spread = [...numbers, 5, 6]

const numbers_without_spread = [numbers, 5, 6]



You'll get an output as

[1, 2, 3, 4, 5, 6]
[[1, 2, 3, 4], 5, 6]

- objects

Now, let's check the same with objects

const human = {
  name : "Kedar"

const human_age_with_spread = {
  age : '21'

const human_age_without_spread = {
  age : '21'


you get the output as

[object Object] {
  age: "21",
  name: "Kedar"
[object Object] {
  age: "21",
  human: [object Object] {
    name: "Kedar"


we can say it's a collection remaining elements (hence, the name rest, i.e. “the rest of the elements”) in an array. Its mainly used for merging a list of functional arguments into an array i.e. you can use this when you don't know how many arguments will be passed to your method

Let's have a look at an example

const more_than_three = (...args) => {
  console.log(args)  /* lets see what's in args */
  return console.log(args.filter((x) => x > 3))


You will receive output as

[1, 2, 3, 4, 5, 6, 7, 8]
[4, 5, 6, 7, 8]

WOW! and there you have it

this was just to make things clear. of course, if you wanna deep dive docs spread,rest are the best options.

Hope you are clear with this now
Happy Learning!

Here are few of my other posts which may be helpful to you.

If my posts or resources I share has helped you in some way, please consider buying me a coffee. It will help me pay my tuition fee and encourage me to share amazing and useful stuff with you all.

Top comments (1)

roniemeque profile image

Cool! Another great example for rest usage is when you want to create a new object based on a existing one while excluding specific properties:
const {password, ...userWithoutPassword} = user;


11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields


Read the whole post now!