Explain the difference between spread and rest operator
Utilisateur anonyme
The spread operator is used to spread the elements of an iterable like object or array. For instance, if we wish to find the largest number in an array (say [ 4, 5, 6 ]), using Math.max([4, 5, 6]) will not yield the correct result as Math.max expects comma-separated inputs, and not an array. Here, to spread or separate the elements of the array, we can use the '...' operator this way: Math.max(...[4, 5, 6]) The rest operator, which looks the same as the spread operator, does quite the opposite of it. In the case of a function, for example, the rest operator can be used to combine all the arguments passed to the function into an array. Eg., function(...args) { // here args will be an array of all the arguments passed to the function }. Similarly, if we wish to separate out a property from an object and combine the others, we can use: const singleObject = { propertyOne: 1, propertyTwo: 2, propertyThree: 3} const {propertyOne, ...theOtherProperties} = singleObject; Here theOtherProperties will be an object containing propertyTwo, and propertyThree.