スプレッド構文は、配列やオブジェクトの中身を展開(分解)する構文です。 主に以下のような使い方があります:
const newArray = [...array]; const newObject = { ...object };
コピペではなく実際に打ち込んで練習してみましょう。
const arr1 = [1, 2, 3]; const arr2 = [...arr1]; console.log(arr2); // [1, 2, 3] console.log(arr1 === arr2); // false(別の配列)
const a = [1, 2]; const b = [3, 4]; const merged = [...a, ...b]; console.log(merged); // [1, 2, 3, 4]
const nums = [2, 3]; const extended = [1, ...nums, 4]; console.log(extended); // [1, 2, 3, 4]
const user = { name: "Taro", age: 25 }; const copied = { ...user }; const updated = { ...user, age: 30 }; console.log(copied); // { name: "Taro", age: 25 } console.log(updated); // { name: "Taro", age: 30 }
const numbers = [3, 5, 9]; console.log(Math.max(...numbers)); // 9