2 ms·
JS admits that ... is two operators ("spread" and "rest"), but that they are essential related/cousins (dual in mathematics terminology) and it works well for b
by WorldMaker 10y ago
JS admits that ... is two operators ("spread" and "rest"), but that they are essential related/cousins (dual in mathematics terminology) and it works well for both of them to look the same.
Rest is the variadic case:
function myFunction(a, b, ...rest) {
}
But rest can also be used for destructuring, such as:
let [a, b, ...rest] = ['a', 'b', 'c', 'd']
Or
let { a, b, ...rest } = { a: 'a', b: 'b', c: 'c', d: 'd' }
In this case it's a extra-bang-for-the-buck expansion of variadic parameters into something more generally useful: tearing apart lists (and now objects). From that light, spread is the "structuring" side of rest, in the left-hand/right-hand world of destructuring/structuring.