Spread Operator in JavaScript | IndianTechnoEra - IndianTechnoEra
Latest update Android YouTube

Spread Operator in JavaScript | IndianTechnoEra

The spread operator is three dots (...) that perform several different tasks.


First, the spread operator allows us to combine the contents of arrays. For example, if we two arrays, we could make a third array that combines the two arrays into one.


Example 1:

var arr1 = ['el11', 'el12', 'el13']
var arr2 = ['el21', 'el22', 'el23']
var arr3 = [...arr1, ...arr2] // merge arr1 and arr2 into arr3
console.log(arr3) // [ 'el11', 'el12', 'el13', 'el21', 'el22', 'el23' ]
console.log(arr3.reverse()) //[ 'el23', 'el22', 'el21', 'el13', 'el12', 'el11' ]
var [first] = arr3  // first element of arr3 el12
var [last] = arr3.reverse() // first element of reversed arr3 el23

var cleanA3 = arr3.join(',') // remove braces and inverted commas and separated with , character
console.log(cleanA3)   // ar23,ar22,ar21,ar13,ar12,ar11

var [...reset] = arr1 // print all element of arr1 left to the number of word before separated with commas before three dot
console.log(reset)  //[ 'el12', 'el13' ]

var [,first,...reset] = arr1
console.log(reset)  //[ 'el13' ]

Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.