Collect the remaining array values with rest syntax.

rest-values In an array pattern, `...tail` collects the remaining values into a new array. Use it when the first values have special meaning and the rest are a group.

Rest Values

firstScore
rest_values.js
Replay: real traced execution (multi-file project)
const firstScore = 4;
const [head, ...tail] = [firstScore, 2, 3];
const tailTotal = tail.reduce((sum, value) => sum + value, 0);
const total = head + tailTotal;

console.log("head=" + head);
console.log("total=" + total);
const firstScore = 1;
const [head, ...tail] = [firstScore, 2, 3];
const tailTotal = tail.reduce((sum, value) => sum + value, 0);
const total = head + tailTotal;

console.log("head=" + head);
console.log("total=" + total);
const firstScore = 7;
const [head, ...tail] = [firstScore, 2, 3];
const tailTotal = tail.reduce((sum, value) => sum + value, 0);
const total = head + tailTotal;

console.log("head=" + head);
console.log("total=" + total);
  1. firstScore ← 4, head ← 4, tail ← 2,3, tailTotal ← 5, total ← 9

    1const firstScore→ 4 = 4; //@firstScore=1, 72const [head→ 4, ...tail→ 2,3] = [firstScore4, 2, 3];3const tailTotal→ 5 = tail2,3.reduce((sum, value) => sum + value, 0);4const total→ 9 = head→ 4 + tailTotal5;56console.log("head=" + head4);7console.log("total=" + total9);
    outputhead=4
    total=9
  1. firstScore ← 1, head ← 1, tail ← 2,3, tailTotal ← 5, total ← 6

    1const firstScore→ 1 = 1;2const [head→ 1, ...tail→ 2,3] = [firstScore1, 2, 3];3const tailTotal→ 5 = tail2,3.reduce((sum, value) => sum + value, 0);4const total→ 6 = head→ 1 + tailTotal5;56console.log("head=" + head1);7console.log("total=" + total6);
    outputhead=1
    total=6
  1. firstScore ← 7, head ← 7, tail ← 2,3, tailTotal ← 5, total ← 12

    1const firstScore→ 7 = 7;2const [head→ 7, ...tail→ 2,3] = [firstScore7, 2, 3];3const tailTotal→ 5 = tail2,3.reduce((sum, value) => sum + value, 0);4const total→ 12 = head→ 7 + tailTotal5;56console.log("head=" + head7);7console.log("total=" + total12);
    outputhead=7
    total=12