How do I use the J and x variables in Thunkable?

Hi
I am trying to google around and want to learn about the J and X variables in Thunkable. How are they ment to be used?

ex, “For each Item J in list, do”…
I understand it’s a loop but what does the J stands for?

Thanks!

1 Like

The “j” is just a variable that holds each list item as the loop iterates.

So let’s say you have a list with items {1,22,83}. If you run this code:

For each item j in list do
     set label's text to j
     wait 1 second

Then you will see…
1
22
83

…with a second pause between each number.

2 Likes

Hi.
Thanks for the explanation! So I use this within an existing list if I want to do anything with the items in that list? And the J variable will hold these items until I have told what to do with them?
Is that correct?

Does it stands for anything (the J) or is it just like a x/y in a diagram?

Thanks!

Yes, that’s correct.

The “j” does not stand for anything. It’s similar to x or y as a math variable. In fact, you can create your own loop variables and call them whatever you want. But it’s easiest to just use the default “i” or “j” variables.

Great, thanks!

I have noticed there is an ”x” in that block as well.
Is that the same thing as the ”j”? Is it to use different loops with the same material/list/items?

Hi there,

The j variable is a local variable used in loops, as seen in the Control blocks.

The x variable is a local variable used in functions.

Both of these can be renamed, those are just the default names.

2 Likes

Thanks Jane! Just out of curiosity, are these variabels “hardcoded” to functions/loops? I mean, is it just a matter of order or is it simply not not possible to use j for functions and vice versa in a technical perspective ? Thanks

2 Likes

You can use whatever variable you want in whatever place. Click in the variable, for example, “j” and the popup list has a rename option.

image

1 Like

Ok so its just a matter of order then? This is how people likes to use it: x for functions and j for loops?

2 Likes

:+1:

1 Like

These are just conventions! You’ll see similar default naming strategies in guides for other programming languages. Another classic is using i in the ‘count with [i] from [1] to [10] by [1]’ block, also seen in the control blocks.

1 Like