# Mathematical operations on the contents of a variable I've created by looking up a LocalStorage key?

**URL:** <https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600>\
**Category:** Questions about Thunkable X\
**Created:** [September 8, 2018, 12:07am UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600 "2018-09-08T00:07:54Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![RelapseDev](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/relapsedev/32/24765_2.png) [@RelapseDev](https://community.thunkable.com/u/RelapseDev)\
**Post date:** [September 8, 2018, 12:07am UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/1 "2018-09-08T00:07:54Z")

</div>

Hi, whatever I seem to do, I can’t seem to be able to perform mathematical operations (or boolean operations, or any other kind) on data I put into a variable, when I get this data from LocalStorage. I can display the data fine, so I know it’s there, but when I try to use it in an if/do, or a mathematical expression, it returns NaN or null.

Please help! Example below.

 ![math1](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/2/225478cffbc262c191c5fd8d11025de770789338.PNG) ![math2](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/a/1/a10ece5d11fe49f6f92011132ffc4f2e6a5108bb.PNG)

---

<div class="post-metadata">

**Author:** ![actech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@actech](https://community.thunkable.com/u/actech)\
**Post date:** [September 8, 2018, 9:02am UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/2 "2018-09-08T09:02:52Z")

</div>

Hi,

Operations on the value obtained from the LocalStorage must be performed in the “then do” part. Blocks with the “then do” part are asynchronous blocks.

---

<div class="post-metadata">

**Author:** ![jan\_hrabos](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@jan\_hrabos](https://community.thunkable.com/u/jan_hrabos)\
**Post date:** [September 8, 2018, 3:41pm UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/3 "2018-09-08T15:41:04Z")

</div>

![Bez%20n%C3%A1zvu1](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/4/1/4133c06bab508deff6d4e2b7b9e3444ea74acaef.jpg)

 ![Bez%20n%C3%A1zvu](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/1/01177e0df5de60183397f1dbcdcef98f6f5b3d0d.jpg)

answer from actech in blocks 🙂

---

<div class="post-metadata">

**Author:** ![RelapseDev](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/relapsedev/32/24765_2.png) [@RelapseDev](https://community.thunkable.com/u/RelapseDev)\
**Post date:** [September 8, 2018, 5:23pm UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/4 "2018-09-08T17:23:05Z")

</div>

Is there no way to assign the value obtained to a new variable and perform operations on that variable later on?

---

<div class="post-metadata">

**Author:** ![actech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@actech](https://community.thunkable.com/u/actech)\
**Post date:** [September 9, 2018, 6:20am UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/5 "2018-09-09T06:20:00Z")

</div>

This can be done. But assign the value of the new variable to the inside of the function “do something”.

Mobile programming differs from desktop programming in that it uses the concept of asynchronous programming. Look at the difference between the synchronous function call from asynchronous to better understand this.

---

<div class="post-metadata">

**Author:** ![shriram](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@shriram](https://community.thunkable.com/u/shriram)\
**Post date:** [September 9, 2018, 6:17pm UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/6 "2018-09-09T18:17:38Z")

</div>

Here’s an example showing how:

> [@How to nest gets from Local Storage?](http://community.thunkable.com/t/how-to-nest-gets-from-local-storage/45052/2):
>
> You could just create variables and assign value to the variable before the second call. Something like: [28] Regards Rob

---

<div class="post-metadata">

**Author:** ![RelapseDev](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/relapsedev/32/24765_2.png) [@RelapseDev](https://community.thunkable.com/u/RelapseDev)\
**Post date:** [September 9, 2018, 11:56pm UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/7 "2018-09-09T23:56:52Z")

</div>

Hmmm… The problem is I don’t want to use this procedure to display text. I just want the procedure to go get the value from LocalStorage and put it in a (numerical) variable, then come back.

See, I need to get a bunch of these values from different places, perform operations on them in different procedures, and then display the answers separately, later on.

The whole reason I’m doing this is because it seems like using local storage is the only way to do global variables. Basically that is what I want- a workaround for the fact that there are no global variables in thunkable x.

It seems like I’m missing something. Is the only way to do this really to nest the entire mathematical operation in the then do, every time?? That seems really inelegant, but maybe that’s the only solution here?

---

<div class="post-metadata">

**Author:** ![Mark](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/mark/32/18133_2.png) [@Mark](https://community.thunkable.com/u/Mark)\
**Post date:** [September 10, 2018, 10:04pm UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/8 "2018-09-10T22:04:23Z")

</div>

> [@RelapseDev](#):
>
> It seems like I’m missing something. Is the only way to do this really to nest the entire mathematical operation in the then do, every time?? That seems really inelegant, but maybe that’s the only solution here?

You are right that it is inelegant! It’s hard to make asynchronous operations like `'Local_Storage1 call Get'` be elegant and easy for users to understand. We are working on some things that ought to help the situation (like variables that are global to the entire app). Unfortunately I don’t have an ETA yet on when that will be available.

-Mark

---

<div class="post-metadata">

**Author:** ![RelapseDev](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/relapsedev/32/24765_2.png) [@RelapseDev](https://community.thunkable.com/u/RelapseDev)\
**Post date:** [September 10, 2018, 11:31pm UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/9 "2018-09-10T23:31:02Z")

</div>

OK. So I guess there’s no way to do it.

It’s super weird to me that we can get the contents of LocalStorage into a textbox for display, but not actually get them into a variable for performing math or logic on.

No workaround at all?

---

<div class="post-metadata">

**Author:** ![Mark](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/mark/32/18133_2.png) [@Mark](https://community.thunkable.com/u/Mark)\
**Post date:** [September 11, 2018, 6:24pm UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/10 "2018-09-11T18:24:49Z")

</div>

> [@RelapseDev](#):
>
> OK. So I guess there’s no way to do it.
> 
> It’s super weird to me that we can get the contents of LocalStorage into a textbox for display, but not actually get them into a variable for performing math or logic on.
> 
> No workaround at all?

The way to do it is to ensure that all the work that you want to do with the value that you get from LocalStorage is done within the ‘`then do`’ slot of the ‘`Local_Storage1 call Get`’ block.

One way to think about these ‘asynchronous’ blocks is that blocks like ‘`Local_Storage1 call Get`’ are performed in parallel (i.e. at the same time) as the blocks that come after them. There is no guarantee that the asynchronous block will be done with its work before those blocks which come after them. That is why accessing the `enterednumber` variable after your call to the `do something` block doesn’t work - the `do something` block hasn’t yet set the `enterednumber` variable. The ‘`then do`’ slot is there specifically to provide a way to do further work **after the ‘`Local_Storage1 call Get`’ has finished retrieving its value**.

> [@RelapseDev](#):
>
> See, I need to get a bunch of these values from different places, perform operations on them in different procedures, and then display the answers separately, later on.

You will need to get those other values in blocks that are executed within the ‘`then do`’ slot. You can call procedure blocks within ‘`then do`’ slot, which can help with splitting up the work. Note, however, that if those procedures contain blocks with ‘`then do`’ slots you will have to ensure that any further processing involving those blocks’ values are done within their ‘`then do`’ slots.

We know that it can get awkward to program this way and we are working on ways to make it less awkward.

-Mark

---

<div class="post-metadata">

**Author:** ![RelapseDev](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/relapsedev/32/24765_2.png) [@RelapseDev](https://community.thunkable.com/u/RelapseDev)\
**Post date:** [September 11, 2018, 8:00pm UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/11 "2018-09-11T20:00:25Z")

</div>

OK, got it. I’ll try it this way. I’m just relieved that I’m not going crazy- thought I was missing something simple! Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Mark](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/mark/32/18133_2.png) [@Mark](https://community.thunkable.com/u/Mark)\
**Post date:** [September 11, 2018, 8:16pm UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/12 "2018-09-11T20:16:22Z")

</div>

Glad to help!

-Mark

---

<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, 11:59am UTC](https://community.thunkable.com/t/mathematical-operations-on-the-contents-of-a-variable-ive-created-by-looking-up-a-localstorage-key/45600/13 "2024-11-08T11:59:45Z")

</div>


