Reference vs Copy in JS

Primitives

Strings numbers and booleans are copied by value. If we'll change one variable, the other variable will not change.

Arrays

Arrays are copied by reference.It means, that if one of these arrays will be changed, the other also will be changed. It works like that, because in variable we store the reference to the particular array

If we need a copy of array by value we have 4 options:

Objects

Objects also are copied by reference.It means, that if one of these objects will be changed, the other also will be changed. It works like that, because in variable we store the reference to the particular object

If we need a copy of object by value, we can use this method

const obj2 = Object.assign({}, obj, {newProp:34, originalProp: newValue})