The spread operator was first introduced in the ES6 JavaScript Standard. The spread operator literally spreads the contents of an array into its elements, which makes operations like concatenation and array manipulation easier.
Let’s say we want to concatenate two arrays; we either do this by using the concat
function, like this:
a = [1,2,3]b = [4,5,6]c = a.concat(b)console.log("c: ", c)
Or, we can do the same thing using the spread operator:
const a = [1,2,3]const b = [4,5,6]const c = [...a, ...b] //spread operatorconsole.log("c: ", c)
[...a, 'something', ...b];
.b = [...a];
.b = {...a};
.const person = { name: "Mike", age: "21"};const student = { id: "109", cgpa: "3.5"};const new_object = { ...person, ...student, major: "Computer Science", academic_level: "Junior"};console.log(new_object);
Free Resources