Skip to content

Commit c31e40b

Browse files
authored
Update variable reference pages (#6024)
* create/set variables gifs * update variable/type pages
1 parent 6b4a9a2 commit c31e40b

File tree

6 files changed

+123
-38
lines changed

6 files changed

+123
-38
lines changed

docs/blocks/variables/assign.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
11
# Assignment Operator
22

3-
Use an equals sign to make a [variable](/blocks/variables/var) store the [number](/types/number)
4-
or [string](/types/string) you say.
3+
Use an equals sign to make a [variable](/blocks/variables/var) store a [number](/types/number), [string](/types/string), or other [type](/types) of value.
54

6-
When you use the equals sign to store something in a variable, the equals sign is called
5+
When you use the equals sign (**=**) to store something in a variable, the equals sign is called
76
an *assignment operator*, and what you store is called a *value*.
87

8+
When you work in JavaScript or Python the equals sign is used:
9+
10+
```typescript-ignore
11+
item = 3
12+
```
13+
14+
## The 'set' block
15+
16+
In blocks, a variable assignment happens with the ``||variables:set||`` block when you set a value to a [variable](/blocks/variables/var).
17+
18+
```block
19+
let item = 3
20+
```
21+
22+
### ~ hint
23+
24+
#### Setting a variable using the Toolbox
25+
26+
In the ``||variables:Variables||`` category of the **Toolbox** you can create or choose a variable to assign:
27+
28+
![Setting a variable to a value](/static/blocks/variables/assign.gif)
29+
30+
### ~
31+
932
## Storing numbers in variables
1033

1134
This program makes the variable `item` equal `5` and then shows it on the [LED screen](/device/screen).

docs/blocks/variables/var.md

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,77 @@
1-
# Local Variables
1+
# Declaring variables
22

3-
How to define and use local variables.
3+
How to declare and use variables.
44

55
## @parent language
66

7-
A variable is a place where you can store and retrieve data. Variables have a name, a [type](/types), and value:
7+
A variable is a place where you can store and retrieve data. Variables have a name, a [type](/types), and a value:
88

99
* *name* is how you'll refer to the variable
10-
* *type* refers to the kind of data a variable can store
11-
* *value* refers to what's stored in the variable
10+
* *type* is the kind of data a variable can store
11+
* *value* is what's stored in the variable
1212

13-
## Var statement
13+
## The variable (var) statement
1414

15-
Use the Block Editor variable statement to create a variable
16-
and the [assignment operator](/blocks/variables/assign)
17-
to store something in the variable.
15+
A variable is created using a `variable` statement. The variable will "declare" itself in this statement. In MakeCode JavaScript a variable is declared along with its first [assignment](/blocks/variables/assign):
1816

19-
For example, this code stores the number `2` in the `x` variable:
17+
```typescript
18+
let x = 2
19+
```
20+
21+
With Python a variable is declared when it's first used. In this case the variable statement is just the assignment of the variable:
22+
23+
```python
24+
x = 2
25+
```
26+
27+
If a variable is declared in blocks you will see the ``||variables:set||`` block with the first use of the variable. This code stores the number `2` in the `x` variable:
2028

2129
```blocks
22-
let x = 2;
30+
let x = 2
2331
```
24-
Here's how to define a variable in the Block Editor:
2532

26-
1. Click `variables`.
33+
The new variable is created inside the ``||variables:Variables||`` category of the **Toolbox**.
34+
35+
### ~ hint
2736

28-
2. Change the default variable name if you like.
37+
#### Creating a variable from the Toolbox
2938

30-
3. Drag a block type on the right-side of the [assignment operator](/blocks/variables/assign) and click the down arrow to change the variable name.
39+
In the ``||variables:Variables||`` category of the **Toolbox** you can create new variable:
40+
41+
![Create a new variable](/static/blocks/variables/create.gif)
42+
43+
Here's how to create a variable using the Toolbox:
44+
45+
1. Click ``||variables:Variables||`` in the Toolbox.
46+
2. Click on **Make a Variable...**.
47+
3. Choose a name for your variable, type it in, and click **Ok**.
48+
4. Drag the new variable, ``||variables:set||`` or ``||variables:change||`` block into your code.
49+
50+
### ~
51+
52+
### Quick example
3153

3254
A variable is created for the number returned by the [brightness](/reference/led/brightness) function.
3355

3456
```blocks
35-
let b = led.brightness();
57+
let b = led.brightness()
3658
```
3759

3860
## Using variables
3961

4062
Once you've defined a variable, just use the variable's name whenever you need what's stored in the variable. For example, the following code shows the value stored in `counter` on the LED screen:
4163

4264
```blocks
43-
let counter = 1;
44-
basic.showNumber(counter);
65+
let counter = 1
66+
basic.showNumber(counter)
4567
```
4668

4769
To change the contents of a variable use the assignment operator. The following code sets `counter` to 1 and then increments `counter` by 10:
4870

4971
```blocks
50-
let counter = 1;
51-
counter = counter + 10;
52-
basic.showNumber(counter);
72+
let counter = 1
73+
counter = counter + 10
74+
basic.showNumber(counter)
5375
```
5476

5577
## Why use variables?
@@ -58,11 +80,11 @@ If you want to remember and modify data, you'll need a variable.
5880
A counter is a great example:
5981

6082
```blocks
61-
let counter = 0;
62-
input.onButtonPressed(Button.A, () => {
63-
counter = counter + 1;
64-
basic.showNumber(counter);
65-
});
83+
let counter = 0
84+
input.onButtonPressed(Button.A, function () {
85+
counter = counter + 1
86+
basic.showNumber(counter)
87+
})
6688
```
6789

6890
## Local variables
@@ -72,16 +94,38 @@ Local variables exist only within the function or block of code where they're de
7294
```blocks
7395
// x does NOT exist here.
7496
if (led.brightness() > 128) {
75-
// x exists here
76-
let x = 0;
97+
// x exists here
98+
let x = 0
7799
}
78100
```
79101

80-
### Notes
102+
## Notes about variables
103+
104+
### Variable names
81105

82-
* You can use the default variable names if you'd like, however, it's best to use descriptive variable names. To change a variable name in the editor, select the down arrow next to the variable and then click "new variable".
106+
Some blocks come from the Toolbox with default variable names, such as `list` from ``||arrays:Arrays||``.
107+
You can use the default variable names if you like, however, it's best to use descriptive variable names. To change a variable name in the editor, select the down arrow next to the variable and then click **"Rename variable..."** to change it.
108+
109+
### Hidden declaration block
110+
111+
If a variable is used more than once, and its declaration block sets the variable to `0`, that first declaration block is hidden. For example:
112+
113+
```blocks
114+
let x = 0
115+
if (x == 0) {
116+
x = 9
117+
}
118+
```
119+
120+
You don't see any first block setting the variable `x` to `0` but that happens in its hidden declaration statement. You'll see the declaration statement, `let x = 0`, if you switch from Blocks to JavaScript in the Editor:
121+
122+
```typescript-ignore
123+
let x = 0
124+
if (x == 0) {
125+
x = 9
126+
}
127+
```
83128

84129
## See also
85130

86131
[types](/types), [assignment operator](/blocks/variables/assign)
87-
195 KB
Loading
262 KB
Loading
378 KB
Loading

docs/types/string.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
# @extends
22

3-
## #intro
3+
## #create
44

5-
## ~ hint
5+
In the ``||variables:Variables||`` category of the **Toolbox** you can create new variable:
66

7-
For the @boardname@, ASCII character codes 32 to 126 are supported; letters, digits, punctuation marks, and a few symbols. All other character codes appear as a ? on the [LED screen](/device/screen).
7+
![Create a new string variable](/static/blocks/variables/string.gif)
88

9-
## ~
9+
Here's how to create a string variable using the Toolbox:
10+
11+
1. Click ``||variables:Variables||`` in the Toolbox.
12+
2. Click on **Make a Variable...**.
13+
3. Choose a name for your variable, type it in, and click **Ok**.
14+
4. Drag the new ``||variables:set||`` block into your code.
15+
5. Click on the ``||text:Text||`` drawer in the Toolbox and find the ``||text:" "||`` block.
16+
6. Drag the ``||text:" "||`` block into the value slot in of your variable ``||variables:set||`` block.
17+
18+
## Characters you use in strings #custom
19+
20+
### ~ hint
21+
22+
#### Character sets
23+
24+
The available characters to use for a language is called the _character set_. Each character in the set has a number code to match it with.
25+
To display characters on the [LED screen](/device/screen), the @boardname@, uses the "ASCII" character codes of `32` to `126`; letters, digits, punctuation marks, and a few symbols. All other character codes appear as a `?` on the LED screen.
26+
27+
### ~

0 commit comments

Comments
 (0)