# Add item to an array variable with javascript and it changes the default value of the array

**URL:** <https://community.weweb.io/t/add-item-to-an-array-variable-with-javascript-and-it-changes-the-default-value-of-the-array/368>\
**Category:** How do I?\
**Created:** [April 5, 2022, 2:06am UTC](https://community.weweb.io/t/add-item-to-an-array-variable-with-javascript-and-it-changes-the-default-value-of-the-array/368 "2022-04-05T02:06:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinwasie](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/kevinwasie/32/402_2.png) [@kevinwasie](https://community.weweb.io/u/kevinwasie)\
**Post date:** [April 5, 2022, 2:06am UTC](https://community.weweb.io/t/add-item-to-an-array-variable-with-javascript-and-it-changes-the-default-value-of-the-array/368/1 "2022-04-05T02:06:33Z")

</div>

I have the following Custom Javascript in a workflow ation to add an item to an array variable.

_variables[/ VariableName / ‘123456789’].push({foo:“bar”})_

When I run the code in the page, or with the test action link, it automatically sets the **Default Value** of the array variable as well as the value.

Why does the code change the default value. How do I avoid this?

---

<div class="post-metadata">

**Author:** ![aurelie](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/aurelie/32/13249_2.png) [@aurelie](https://community.weweb.io/u/aurelie)\
**Post date:** [April 5, 2022, 9:01am UTC](https://community.weweb.io/t/add-item-to-an-array-variable-with-javascript-and-it-changes-the-default-value-of-the-array/368/2 "2022-04-05T09:01:20Z")

</div>

Hi Kevin,  
this is because you are updating the array itself instead of setting the value to a new array.  
In no code/formula we are taking care for you to treat variable as immutable. If you are going to the js way, we need to be carefull about it

`variables['123'] = [...variables['123'], {foo: "bar"}]`

If you are not familiar with this synthax, we are just creating a new array and assign it to the var. When you do that, magic of Weweb can happen 🙂 (always assign directly the value to the var)

This is also true for object, never update a subkey of a var, always create a new object.

If you want you can also use our safe formula method, even in JS

`variables['123'] = wwFunctions.add(variables['123'], {foo: "bar"})`

Hope it helps, let me know if you need more information

---

<div class="post-metadata">

**Author:** ![Quentin](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/quentin/32/6_2.png) [@Quentin](https://community.weweb.io/u/Quentin)\
**Post date:** [April 5, 2022, 9:20am UTC](https://community.weweb.io/t/add-item-to-an-array-variable-with-javascript-and-it-changes-the-default-value-of-the-array/368/3 "2022-04-05T09:20:05Z")

</div>

You can even use only nocode formulas, like this 👇

 ![CleanShot 2022-04-05 at 11.19.40](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/1X/78544944a74bf11ca979b2c7a6c6336c1407bf31.png)

---

<div class="post-metadata">

**Author:** ![kevinwasie](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/kevinwasie/32/402_2.png) [@kevinwasie](https://community.weweb.io/u/kevinwasie)\
**Post date:** [April 5, 2022, 8:31pm UTC](https://community.weweb.io/t/add-item-to-an-array-variable-with-javascript-and-it-changes-the-default-value-of-the-array/368/4 "2022-04-05T20:31:09Z")

</div>

Awesome, Thank you! That makes sense and will take some getting used to remembering.
