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

kedark

Kedar Kodgire

Posted on March 25, 2020

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 😅.


spread

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']
Enter fullscreen mode Exit fullscreen mode

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]

console.log(numbers_with_spread)

console.log(numbers_without_spread)
Enter fullscreen mode Exit fullscreen mode

You'll get an output as

[1, 2, 3, 4, 5, 6]
[[1, 2, 3, 4], 5, 6]
Enter fullscreen mode Exit fullscreen mode

- objects

Now, let's check the same with objects

const human = {
  name : "Kedar"
}

const human_age_with_spread = {
  ...human,
  age : '21'
}

const human_age_without_spread = {
  human,
  age : '21'
}


console.log(human_age_with_spread)
console.log(human_age_without_spread)
Enter fullscreen mode Exit fullscreen mode

you get the output as


[object Object] {
  age: "21",
  name: "Kedar"
}
[object Object] {
  age: "21",
  human: [object Object] {
    name: "Kedar"
  }
}
Enter fullscreen mode Exit fullscreen mode

rest

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))
}

more_than_three(1,2,3,4,5,6,7,8)
Enter fullscreen mode Exit fullscreen mode

You will receive output as

[1, 2, 3, 4, 5, 6, 7, 8]
[4, 5, 6, 7, 8]
Enter fullscreen mode Exit fullscreen mode

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.

💖 💪 🙅 🚩
kedark
Kedar Kodgire

Posted on March 25, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related