re: Share a quick tip you learned about web dev this year VIEW POST

re: you can clone an array in javascript with [...Array] thank you for mr awais for this i dont know his dev.to username but wherever you are thanks

Can you show an example of how you would do that in javascript?

Thanks for sharing!


really? that was super simple bro

//lets say you have an array like this
let animal=['panda','cat','mongoose'];

//then if you want to clone it one of the method i copy from mr awais is this one
let animalClone=[...animal]

as simple as that

Sweet. Definitely useful! Thanks

i found it here the original post

You can do the same with objects. Keep in mind, this doesn't deep clone. So if you intend to change the values in any way, you'll still be mutating the pointer, and by consequence, the original object.

Code of Conduct Report abuse