DEV Community

Devraj Sawant
Devraj Sawant

Posted on

2. Spread Operator

const numbers = [1,2,3]
console.log(numbers) // [1,2,3]
console.log(...numbers) // 1,2,3
Enter fullscreen mode Exit fullscreen mode

As you can see in above example, console logging the numbers gives the entire array but logging the same 'numbers' array with spread operators gives us the element inside an array and that is what spread operator is used for.

spread operator is a special feature of Javascript which helps to expand and get the elements of any iterables such as array,string,etc.

Spread simplifies multiple task such as concatenation, copying, etc

const arr1 = [1, 2, 3];
const copy = [...arr1]; // copy arr1 into copy -> [1,2,3]
const merged = [...arr1,...copy] // merge both in 1 -> [1,2,3,1,2,3]
Enter fullscreen mode Exit fullscreen mode

Thank you.

Top comments (0)