Question: I have created an object and assigned it to 3 variables like so

```
var barChartOptions, longChartOptions, scatterOptions;
barChartOptions = longChartOptions = scatterOptions = {
    legend: {
        display: false
    },
    scales: {
        ...
    }
};

```

also tried

```
var barChartOption = longChartOptions = scatterOptions = {
    legend: {
        display: false
    },
    scales: {
        ...
    }
};

```

Then I tried to add a key to one of the variables. This key is only specific to this one variable and rest of the object is same for all the variables.

```
longChartOptions.aspectRatio = 3;

```

Now, all three variables have the key `aspectRatio`. Why is this happening? I understand if I define the three variables seperately the problem will not occur but the object is quite long and I wanna keep the code DRY.

Answer: 
You can use the spread operator to copy an object to another variable.

```
var barChartOption = {...longChartOptions} = {...scatterOptions} = {
    legend: {
        display: false
    },
    scales: {
      // ...
    }
};

```

Actually, when you use = sign to copy an object to a variable javascript copies the reference of the object to that variable. So when you change anything on any of the variable it also reflects the other one.

To get rid of this you can use the spread operator or the process mentioned by @CertainPerformance.
Make a function that returns the object, and call that function when defining each variable:

```
const makeOptions = () => ({
  legend: {
    display: false
  },
  scales: {
    // ...
  }
});
const barChartOptions = makeOptions(),
      longChartOptions = makeOptions(),
      scatterOptions = makeOptions();

```

> 
> Why is this happening?
> 
> 
> 

Because assignments resolve to the assigned value. Your original code:

```
barChartOptions = longChartOptions = scatterOptions = { /* ... */ };

```

is equivalent to:

```
scatterOptions = { /* ... */ };
longChartOptions = scatterOptions; // references same object
barChartOptions = longChartOptions; // references same object
// so in the end, scatterOptions === barChartOptions

```

Each variable name references the same object in memory. No cloning of the structure occurs when you assign the same object to a different variable name.