DEV Community

Discussion on: Javascript Clean Code Tips & Good Practices

Collapse
 
nombrekeff profile image
Keff

Nice list, quite complete.

Just one thing, in the copy array example, the spread operator does a shallow-copy. It will copy things like strings, numbers, but not objects or other arrays:

let obj1 = { name: 'test' };
let obj2 = { name: 'test2' };

let array = [obj1, obj2];
let copy = [...array];

array[0] === copy[0] // true

// Or if you modify copy, it will affect the original items in `array` 
copy[0].name = 'Other name';

array[0].name // 'Other name'

Enter fullscreen mode Exit fullscreen mode

This can lead to some weird behaviour, and some tricky bugs. I've had some problems with this in the past, and it took a while to fix. Just a thing to notice!!

But it's not a big deal, and a simple fix!

Collapse
 
apoorvtyagi profile image
Apoorv Tyagi

Oh this can lead to some problems, yes. I didn't know that. Thanks for sharing this 👍