# Variable not found

**URL:** <https://discourse.processing.org/t/variable-not-found/3541>\
**Category:** Beginners\
**Created:** [September 14, 2018, 10:15pm UTC](https://discourse.processing.org/t/variable-not-found/3541 "2018-09-14T22:15:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blue\_Obsidian](https://avatars.discourse-cdn.com/v4/letter/b/c5a1d2/32.png) [@Blue\_Obsidian](https://discourse.processing.org/u/Blue_Obsidian)\
**Post date:** [September 14, 2018, 10:15pm UTC](https://discourse.processing.org/t/variable-not-found/3541/1 "2018-09-14T22:15:35Z")

</div>

I have a problem with my code. At line 105, it doesn’t find p1 and I don’t know why, so can you help me please?  
Here’s the link:  
[https://editor.p5js.org/Dark\_Obsidian/sketches/H1krCM\_\_Q](https://editor.p5js.org/Dark_Obsidian/sketches/H1krCM__Q)

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [September 14, 2018, 10:25pm UTC](https://discourse.processing.org/t/variable-not-found/3541/2 "2018-09-14T22:25:41Z")

</div>

You’re declaring your `p1` variable inside the `setup()` function, so it’s only in scope inside the `setup()` function. Try to put together a simpler example:

```auto
function setup(){
  let x = 'hello';
}

function draw(){
  console.log(x); // won't work
}

```

If you want to reference a variable in multiple functions, you need to declare your variable outside of the functions:

```auto

let x;

function setup(){
  x = 'hello';
}

function draw(){
  console.log(x);
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 14, 2018, 10:28pm UTC](https://discourse.processing.org/t/variable-not-found/3541/3 "2018-09-14T22:28:55Z")

</div>

```auto
function setup () {
  let p1 = new Player ('z', 's', 50, 1);
  let p2 = new Player ('p', 'm', 425, 2);

```

Variables declared inside a function cease to exist when that function quits. ☠

Unless they become a closure to another inner function. 🤑

---

<div class="post-metadata">

**Author:** ![Blue\_Obsidian](https://avatars.discourse-cdn.com/v4/letter/b/c5a1d2/32.png) [@Blue\_Obsidian](https://discourse.processing.org/u/Blue_Obsidian)\
**Post date:** [September 14, 2018, 10:31pm UTC](https://discourse.processing.org/t/variable-not-found/3541/4 "2018-09-14T22:31:30Z")

</div>

Thanks, I totally forgot that.

---

<div class="post-metadata">

**Author:** ![codingking03](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@codingking03](https://discourse.processing.org/u/codingking03)\
**Post date:** [September 15, 2018, 2:42pm UTC](https://discourse.processing.org/t/variable-not-found/3541/5 "2018-09-15T14:42:08Z")

</div>

if you declare the variable without the datatype/var/let thing it will be globally scoped  
extremely useful for declaring within setup():

```auto
function setup(){
  p1 = new Player (loremipsum);
  p2 = new Player (loremipsum);

}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 15, 2018, 10:25pm UTC](https://discourse.processing.org/t/variable-not-found/3541/6 "2018-09-15T22:25:30Z")

</div>

> [@codingking03](#):
>
> if you declare the variable without the `datatype/var/let` thing it will be globally scoped…

However, if `"strict mode";` is active, accessing undeclared variables throws an exception: 😱

> **[Strict mode - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Converting_mistakes_into_errors)**
>
> JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode". Strict mode isn't just a subset: it intentionally has different semantics from normal code. Browsers not supporting...

> Assignments, which would accidentally create global variables, instead throw an error in strict mode:

```auto
'use strict';
                       // Assuming a global variable mistypedVariable exists
mistypeVariable = 17; // this line throws a ReferenceError due to the 
                       // misspelling of variable

```
