Skip to content

Latest commit

 

History

History
24 lines (16 loc) · 673 Bytes

what_is_a_spread_operator.md

File metadata and controls

24 lines (16 loc) · 673 Bytes

What is a spread operator?

The spread operator (...) is used to expand elements of an array or object. It can be used to create shallow copies or merge multiple arrays or objects.

Example with arrays:

const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5];
console.log(arr2); // [1, 2, 3, 4, 5]

Example with objects:

const obj1 = { name: 'John', age: 30 };
const obj2 = { ...obj1, city: 'New York' };
console.log(obj2); // { name: 'John', age: 30, city: 'New York' }

Tags: basic, JavaScript, operators