Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spread Operator in JavaScript
/* Spread Operator */
// Case 1
const temperatures = [25, 22, 10, 34, 28, 15];
console.log('Min Value: ', Math.min(...temperatures));
console.log('Max Value: ', Math.max(...temperatures));
// Case 2
const someNumbers = ['First Name', 'Last Name', 'Age', 'Gender'];
const [first, ...tail] = someNumbers;
console.log('First Value: ', first);
console.log('Rest of the Array: ', tail);
// Case 3
const group1 = ['Person1', 'Person 2', 'Person 3'];
const group2 = ['Person4', 'Person 5', 'Person 6'];
const allGroups = [...group1, ...group2];
console.log(allGroups);
// Case 4
const colors1 = { a: 'Red', b: 'Green', c: 'Blue' };
const colors2 = { d: 'Yellow', e: 'Orange', f: 'Grey' };
const allColors = {...colors1, ...colors2};
console.log(allColors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment