DEV Community

Cover image for JAVASCRIPT SPREAD OPERATOR
Sam Sonter
Sam Sonter

Posted on

JAVASCRIPT SPREAD OPERATOR

Hi everyone, I am here again with tips on how to write easy simple and clean code.

Going forward I will state an example on how to practice the use of Spread Operator

Look at the code snippet below (using objects & arrays to shallow and copy)

const newObject = {};
Object.keys(oldObject).forEach((key) => {
    newObject[key] = oldObject[key];
});


const newArray =[];
oldArray.forEach((element) => {
   newArray.push(element);
});
Enter fullscreen mode Exit fullscreen mode

You can easily re-write that code in a much more simple way. Life will be much more easier for anyone reading your code.

const newObject = { ...oldOject };
const newArray =[ ...oldArray ];
Enter fullscreen mode Exit fullscreen mode

Thanks for reading and happy coding.
SAM SONTER

Top comments (0)