# how to create arrays in a function

**URL:** <https://forum.kirupa.com/t/how-to-create-arrays-in-a-function/637134>\
**Category:** Uncategorized\
**Created:** [September 5, 2017, 4:17pm UTC](https://forum.kirupa.com/t/how-to-create-arrays-in-a-function/637134 "2017-09-05T16:17:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alphons3k](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@alphons3k](https://forum.kirupa.com/u/alphons3k)\
**Post date:** [September 5, 2017, 4:17pm UTC](https://forum.kirupa.com/t/how-to-create-arrays-in-a-function/637134/1 "2017-09-05T16:17:38Z")

</div>

am working on a function where the input of n, produces an output of n arrays .  
eg arr(1) \>\>\> newArray1=[];  
arr(2) \>\>\>\> newArray1=[]; newArray2=[]  
arr(3) \>\>\>\> newArray1=[];newArray2=[];newArray3=[];

function arr(n){

}

this is what am coming up with, a main array where i pusher other arrays, i can call them by index not by name

var mainArry=[];  
var aYY=[];  
for (var i=1; i\<=n;i++){  
var aYY = [i];  
mainArry.push(aYY );  
}  
}

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [September 5, 2017, 8:24pm UTC](https://forum.kirupa.com/t/how-to-create-arrays-in-a-function/637134/2 "2017-09-05T20:24:59Z")

</div>

You’re looking for something like this?

```
function arr(n) {
	for (var i = 1; i <= n; i++) this["newArray" + i] = [i]
}
arr(3)
newArray2.push("foo")
alert(newArray2) //2, foo
```
