Three dots (...) mean in JavaScript
This is one of the cool new feature of Javascript's ECMA6; ... is one of these new Javascript functionalities.
It can be used in two different ways; as a spread operator OR as a rest parameter
Spread Operator
This is very useful when we want to copy properties of one object in to another but with a bit of modification in value of some attributes.
eg. Let say we have a below object:
const object1 = {
fullName: 'Rahul',
occupation: 'Software developer',
age: 31,
website: 'https://rahul.xyz'
};
and we want to create different objects with only change in name.
We can do this very easily with the help of spread operator :
const object2 = {
...object1
fullName: 'Tom',
}
Otherwise we need to copy object first then we have to change its property. Now its all happening in single step only
Rest Parameter
This I really found very helpful, Sometimes we have to design some Api's which can accept n number of parameters, in those scenarios this can be really useful.
Let me try to explain you with a simple example, We want to design a method to sum n numbers :
function sum(...numbers){
return numbers.reduce((sum, val)=>{
return sum += val;
});
}
Now run this for :
sum(3,5) // gives 8
sum(1,2, 3, 5) //gives 11.
Cool .. right !!!