-2

I was learning how to web scrape in nodejs and came across this kind of array. What is the meaning?

articles = [               //WHAT IS THIS
    ...articles,
    ...new_articles
];
41x3n
  • 5
  • 1
  • 3
  • 1
    It's the spread syntax: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax – Nick Parsons Feb 26 '19 at 13:36

1 Answers1

0

When we see three dots (…) in the code, it's either rest parameters or the spread operator.

Rest parameters: When three dots (…) is at the end of function parameters it will gather the rest of the list of arguments into an array.

spread operator: Expands elements of an array (or all iterables) into places where multiple elements can fit.

yourFunction(arg1, arg2, ...argN) { // used like rest parameter here
  console.log(arg1);
  console.log(arg2);
  console.log(argN);
}

var inputs = ["a", "b", "c", "d", "e", "f"];
yourFunction(...inputs); // used like spread operator here

Another example of the spread-operator:

const array1 = ['item1', 'item2', 'item3'];
const array2 = ['item5', 'item6', 'item7'];

const items = [...array1, 'item4', ...array2];

console.log(items);
// 'item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7'
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
Joel
  • 5,732
  • 4
  • 37
  • 65