# Help a poor noob - arrays

**URL:** <https://discourse.processing.org/t/help-a-poor-noob-arrays/29505>\
**Category:** Beginners\
**Created:** [April 19, 2021, 9:11pm UTC](https://discourse.processing.org/t/help-a-poor-noob-arrays/29505 "2021-04-19T21:11:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luizF](https://avatars.discourse-cdn.com/v4/letter/l/b4bc9f/32.png) [@luizF](https://discourse.processing.org/u/luizF)\
**Post date:** [April 19, 2021, 9:11pm UTC](https://discourse.processing.org/t/help-a-poor-noob-arrays/29505/1 "2021-04-19T21:11:09Z")

</div>

Hello there. I’m realy starting learning. I’m trying change the array I’m using but don’t work put the name in variable… How do i do?

var fase = stage1;

var stage1 = [  
[‘0’, ‘1’, ‘0’, ‘1’],  
[‘0’, ‘1’, ‘1’, ‘0’],  
];  
var stage2 = [  
[‘0’, ‘1’, ‘1’, ‘1’],  
[‘1’, ‘1’, ‘1’, ‘0’],  
];

for (i = 0; i \< fase.length; i++) {  
for (j = 0; j \< fase.length; j++) {  
if (fase[j][i] === ‘0’) {  
image(img, xPos + i \* 40, yPos + j \* 40, 40, 40);  
}

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [April 19, 2021, 9:28pm UTC](https://discourse.processing.org/t/help-a-poor-noob-arrays/29505/2 "2021-04-19T21:28:03Z")

</div>

Hi,

Welcome to the forum! 😉

Your error comes from the fact that you are assigning `stage1` to the variable `fase` before the variable `stage1` has been initialized.

The computer reads your program from top to bottom but in JavaScript it doesn’t give you an error. You can refer to this previous thread on the subject :

> [@We have to claim variables in advance?](https://discourse.processing.org/t/we-have-to-claim-variables-in-advance/29431):
>
> Interestingly, if I claim variable like function setup() { createCanvas(100,100); let pg = createGraphics(100,100); // claim variable like this } function draw() { pg.background(100); image(pg, 0, 0); } when I call pg.background(100), the error occurs “pg is not defined”. But if I claim it in advance like let pg; function setup() { createCanvas(100,100); pg = createGraphics(100,100); // claim variable like this } function draw() { pg.background(100); image(pg, 0, 0); } i…

This is what happens :

1. The JavaScript engine parse your code. Because of [hoisting](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting), it moves the declaration of the `stage1` variable so it’s doing :

```auto
var stage1; // Uninitialized -> undefined

```

1. Then we declare and assign `stage1` to `fase` which is `undefined`:

```auto
var fase = stage1; // -> undefined

```

1. The `stage1` variable gets initialized with the array :

```auto
var stage1 = [
  [‘0’, ‘1’, ‘0’, ‘1’],
  [‘0’, ‘1’, ‘1’, ‘0’],
];

```

But `fase` is still undefined!!

Check out this simple example :

```auto
var a = b;
console.log(a); // Prints undefined

var b = [0, 1];
console.log(a, b); // Prints undefined [0, 1]

```

So you need to assign `stage1` to `fase` after `stage1` has been initialized 😋

---

<div class="post-metadata">

**Author:** ![luizF](https://avatars.discourse-cdn.com/v4/letter/l/b4bc9f/32.png) [@luizF](https://discourse.processing.org/u/luizF)\
**Post date:** [April 19, 2021, 9:31pm UTC](https://discourse.processing.org/t/help-a-poor-noob-arrays/29505/3 "2021-04-19T21:31:35Z")

</div>

thank you so much!! I’ll work on this!

I defined the varfase at setup and it works! Thanks

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [April 19, 2021, 9:37pm UTC](https://discourse.processing.org/t/help-a-poor-noob-arrays/29505/4 "2021-04-19T21:37:29Z")

</div>

You are welcome!

Also note that it’s preferred not to use `var` when declaring variables in JavaScript because it can give you some obscure errors and bugs…

Use `let` or `const` (when the variable won’t be re assigned)

> **[Should You Truly Never Use var?](https://dev.to/johnwolfe820/should-you-never-truly-use-var-bdi)**
>
> Getting to the bottom of the debate about this classic variable
