# Local variable in an event handler

**URL:** https://community.thunkable.com/t/local-variable-in-an-event-handler/863665
**Category:** Questions about Thunkable X
**Created:** [September 30, 2020, 6:46pm UTC](https://community.thunkable.com/t/local-variable-in-an-event-handler/863665 "2020-09-30T18:46:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![john.reedashnf](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@john.reedashnf](https://community.thunkable.com/u/john.reedashnf)
#### Post date: [September 30, 2020, 6:46pm UTC](https://community.thunkable.com/t/local-variable-in-an-event-handler/863665/1 "2020-09-30T18:46:00Z")

</div>

![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/e/0e658562dabd7b6c275677dc1a509175cb47e631.png)  
How can I creat this local variable (x2) in Thunkable? TIA

---

<div class="post-metadata">

### Author: ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)
#### Post date: [September 30, 2020, 10:45pm UTC](https://community.thunkable.com/t/local-variable-in-an-event-handler/863665/2 "2020-09-30T22:45:50Z")

</div>

The most “local” scope in Thunkable is APP. For anything but the most basic/educational apps, this is a massive shortcoming. If you use the narrowest scope (app) and have 10 variables in each screen in an app with 7 screen, there are now 70 variables in the list. Additionally, these variables can be altered by any screen or function within the app. Further violations of “encapsulation” including the general asynchronous execution within Thunkable.

To navigate this landmine of Uber-global variables, I use the following technique:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/e/7/e7621ca3795ac1e72b0e1b7f26daa0baa9f9ce2f.png)

At the beginning of an event or function, I create a dummy loop through an empty list. Then I “initilize” the variable to some value. Both of these steps are required. After this, you can use MyVariable just like in any other programming language.

The loop acts as a declaration, the set acts as an intilization. You need to be meticulous about the declarations AND intilizations, as Thunkable will crash if you reference an “uninitilized” variable.

Happy Thunking!

---

<div class="post-metadata">

### Author: ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)
#### Post date: [October 1, 2020, 12:51am UTC](https://community.thunkable.com/t/local-variable-in-an-event-handler/863665/3 "2020-10-01T00:51:03Z")

</div>

> [@drted](#):
>
> a declaration, the set acts as an intilization. You need to be meticulous about the declarations AND intilizations, as Thunkable will crash if you r

non-persistent data variables  
function type - scoped to the screen they are created in. leave screen and come back = new value  
app type - scoped across screens but only to that that instance of the app running. close app and open again = new value

persistent data  
stored type -scoped across screens. data doesn’t change if you close the app and reopen. but if you delete the app and reinstall, goodbye data.  
cloud type - scoped across screens. if you data is stored properly, this data will persist against installs/deletes

---

<div class="post-metadata">

### Author: ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)
#### Post date: [November 8, 2024, 12:23pm UTC](https://community.thunkable.com/t/local-variable-in-an-event-handler/863665/4 "2024-11-08T12:23:43Z")

</div>


