The following code shows how to use destructing pattern and rest syntax.
var num1, num2, rest; var x, y, z; [num1, num2, ...rest] = [1, 2, 3, 4, 5]; [x, y, z] = [1, 2, 3, 4, 5]; console.log(num1); // 1 console.log(num2); // 2 console.log(rest); // [3, 4, 5] console.log(x, y, z) // 1 2 3
Here, using destructuring we assign values to the variables num1, num2, and rest.
A SyntaxError will be thrown if a trailing comma is used on the left-hand side with a rest element.
var [a, b,] = [1, 2, 3]; console.log(a, b) // 1 2 var [num1, ...num2,] = [1, 2, 3]; // SyntaxError: rest element may not have a trailing comma