# Confused about array behavior

**URL:** <https://forum.kirupa.com/t/confused-about-array-behavior/630017>\
**Category:** flash\
**Created:** [April 23, 2015, 2:41pm UTC](https://forum.kirupa.com/t/confused-about-array-behavior/630017 "2015-04-23T14:41:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlpann](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@tlpann](https://forum.kirupa.com/u/tlpann)\
**Post date:** [April 23, 2015, 2:41pm UTC](https://forum.kirupa.com/t/confused-about-array-behavior/630017/1 "2015-04-23T14:41:45Z")

</div>

I have an array myArray and a class Thing that I pass it to. I also have a button btn that I click to show the value of myArray.

```auto
var myArray:Array= [1,2,3,4,5]
var thing:Thing = new Thing(arr);
btn.addEventListener(MouseEvent:CLICK, traceArray);
function traceArray(m) {trace(myArray)}

```

Help me understand the behavior shown below. I don’t get why setting arr to [] does nothing but using splice does something.

```auto
package {
  public class Thing {
    public function Thing(arr:Array) {
      arr = []; // clicking btn shows 1,2,3,4,5
      arr.splice(0, 2); // clicking btn shows 3,4,5
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [April 23, 2015, 4:40pm UTC](https://forum.kirupa.com/t/confused-about-array-behavior/630017/2 "2015-04-23T16:40:06Z")

</div>

`arr = []` changes the _variable_ not the array itself. The `arr` variable after this line now references a brand new, empty array. The other array still exists - still assigned to myArray outside of the class.

In the case of `arr.splice`, you’re working from the same array myArray points to, because arr is defined as having the same value as myArray when Thing is called. arr and myArray will be the same unless you change arr to be something else.

This can be tricky to get your head around first. You just gotta mentally get a grasp on where things are defined and how `=` changes what a variable is and `.` reaches into a variable and accesses/changes its properties. And when you have an value with properties (objects and arrays), multiple variables can point to the one version of that value.

---

<div class="post-metadata">

**Author:** ![tlpann](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@tlpann](https://forum.kirupa.com/u/tlpann)\
**Post date:** [April 23, 2015, 5:50pm UTC](https://forum.kirupa.com/t/confused-about-array-behavior/630017/3 "2015-04-23T17:50:58Z")

</div>

Thank you senocular! It is very confusing but you have explained it nicely!
