aweber-banner
Use Cases of Spread Operator in JavaScript

Use Cases of Spread Operator in JavaScript

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

Also read, Spread Operator 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 (Shallow Copy)

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];

4. Splitting the word

let string = "welcome";
let split = [...string];
console.log(split);
//['w', 'e', 'l', 'c', 'o', 'm', 'e']

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