Use Cases of Spread Operator with Array in JavaScript

Use Cases of Spread Operator with Array in JavaScript

In this, tutorial we will see some most useful use cases of spread operators in javascript.

1. Use Spread for Merging Array

const array1 = [1, 2, 3];

const array2 = [4, 5, 6];

const mergedArray = [
  ...array1,
  ...array2
];

// [ 1, 2, 3, 4, 5, 6 ] ;

2. Clone Array

const arr1= ['john', 'steev'];
const arr2 = [...arr1];

arr1; // ['john', 'steev']
arr2; // ['john', 'steev']

3.Node List to Array

const nodeList = document.querySelectorAll('img');

const array = [...nodeList];

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Leave a Comment