// Array
const arr = [1, 2, 3];
console.log(...arr); // Outputs: 1 2 3
const newArr = [...arr, 4, 5]; // Combines arrays
console.log(newArr); // [1, 2, 3, 4, 5]
// Function arguments
Math.max(...[10, 20, 30]); // Equivalent to Math.max(10, 20, 30)
// Object
const obj = { a: 1, b: 2 };
const newObj = { ...obj, c: 3 }; // Merges objects
console.log(newObj); // { a: 1, b: 2, c: 3 }
// Function parameters
function sum(...nums) { // Gathers arguments into an array
return nums.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3)); // 6
// Destructuring
const [first, ...rest] = [1, 2, 3, 4];
console.log(first); // 1
console.log(rest); // [2, 3, 4]
// Passing props
const Component = (props) => <div {...props}>Hello!</div>;
// Updating state
const newState = { ...oldState, updatedKey: newValue };
// Cloning arrays/objects
const newArray = [...oldArray, newItem];
const newObject = { ...oldObject, newKey: newValue };