Expr works with values. A value can be a number, text, an object, or a special value such as `none()`.
Variables give names to values so they can be reused later.
speed = 1200; toolName = 'T1'; radius = 3.0;
In this example, `speed`, `toolName`, and `radius` are variables.
Numbers are used for calculations, coordinates, speeds, dimensions, counters, and other numeric values.
10; 3.14; -5; 1200;
Numbers can be used with operators:
toolDiameter = 6; toolRadius = toolDiameter / 2; toolRadius;
The final value is `3`.
Strings are text values.
'Start'; 'Tool change'; 'Probe failed';
Strings can be stored in variables:
message = 'Probing started'; message;
Strings are often used for messages, labels, file names, dialog text, and values passed to functions.
Expr does not need a separate boolean variable type for basic logic. Conditions use truth rules.
For numbers:
if(1) { 'runs'; }; if(0) { 'does not run'; };
Expr also supports `true` and `false` literals. They behave like numeric `1` and `0`.
a = true; b = false;
More condition rules are covered in Operators and conditions.
Assignment stores a value in a variable.
a = 10;
This means: store the value `10` in variable `a`.
After assignment, the variable can be used in later expressions:
a = 10; b = 20; sum = a + b; sum;
The final value is `30`.
You do not need to declare a normal top-level variable before using it.
feed = 800; depth = 2.5; passes = 4;
These assignments create the variables if they do not already exist.
If a variable already exists, assignment changes its value:
a = 10; a = 15; a;
The final value is `15`.
If you read a variable that has no value, Expr returns `none()`.
unknownValue;
`none()` means “no value”. It is useful, but it is not the same as `0` or an empty string.
You will learn more about `none()` in none(), nan(), and errors.
An assignment also produces the assigned value.
a = 5; b = (a = 10); b;
The final value is `10`. Variable `a` is also `10`.
This can be useful, but avoid putting too many assignments into one line. In machine scripts, clear code is usually better than compact code.
Prefer this:
a = 10; b = a;
Instead of this:
b = (a = 10);
A variable can be updated using its previous value.
count = 0; count = count + 1; count = count + 1; count;
The final value is `2`.
Expr also supports increment and decrement operators:
count = 0; count++; count++; count;
The final value is also `2`.
For beginner scripts, `count = count + 1;` is often easier to read than `count++;`.
Expr supports full assignment:
a = a + 5; a = a - 2; a = a * 3;
Expr also supports compound assignment. Compound assignment is a shorter way to update a variable using its current value.
a += 5; // same as: a = a + 5; a -= 2; // same as: a = a - 2; a *= 3; // same as: a = a * 3;
Other supported compound assignment operators are:
a /= 2; a %= 3; a **= 2; a &= 1; a |= 2; a ^= 3; a <<= 1; a >>= 1;
For beginner scripts, the full form is often easier to read. Compound assignment is useful when the update is simple and obvious, such as counters, totals, offsets, and repeated calculations.
Logical compound assignment is not supported:
a &&= b; // not supported a ||= b; // not supported a ^^= b; // not supported
Expr also has local variables created with `set`.
if(1) { set temporaryValue = 42; };
Local variables are useful, but they need scope rules. They are explained later in Scope and set.
For now, use normal assignment with `=` for simple examples.
Read this script and predict the final value:
toolDiameter = 8; toolRadius = toolDiameter / 2; stepOver = toolRadius * 0.5; stepOver;
The final value is `2`.
Next: Operators and conditions