Operator
Operator
Function
BT supports common operators such as arithmetic, comparison, logic, bit operations, assignment, conditions and member access. It is recommended to use parentheses to clarify the order in complex expressions.
Arithmetic operator
| Operator | Description |
|---|---|
+ | Addition or string concatenation. |
- | Subtraction. |
* | Multiplication. |
/ | Division. |
% | Take remainder. |
** | Power operation. |
x = 12 y = 29 % 5 z = 2 ** 3
Comparison operator
| Operator | Description |
|---|---|
> | Greater than. |
< | Less than. |
>= | Greater than or equal to. |
<= | Less than or equal to. |
== | Loose equal. |
!= | Loose does not equal. |
=== | Strictly equal, return true only when the value and type are consistent. |
!== | Strictly not equal. |
price = 19.99 // Output: true print price >= 10 // Output: false print 1 === '1'
Logical operator
| Operator | Description |
|---|---|
&& | Execute and return the right side only if the left side is true, otherwise return the left side directly. |
|| | If the left side is true, return directly to the left side, otherwise execute and return to the right side. |
! | Negate according to BT truth rules. |
?? | Only execute and return to the right side if the left side is null or empty, otherwise return to the left side directly. |
name = input_name ?? 'guest' count = total || 0 user && login(user)
Bit operator
| Operator | Description |
|---|---|
& | Bitwise AND, compare the binary representations of two numbers bit by bit. If the two corresponding bits are both 1, the result of the bit is 1, otherwise it is 0 |
| | Bitwise OR, compare the binary representations of two numbers bit by bit. If at least one of the two corresponding bits is 1, the result of that bit is 1, otherwise it is 0 |
^ | Bitwise XOR, compare the binary representation of two numbers bit by bit. If the two corresponding bits are different, the result of the bit is 1, otherwise it is 0 |
~ | Bitwise inversion, invert the binary representation of a number bit by bit, that is, 0 becomes 1, and 1 becomes 0 |
<< | Shift left, shift all binary bits of a number to the left by a certain number of bits, and fill the empty bits in the sign bit (the leftmost bit) with 0 |
>> | Shift right, shift all binary digits of a number to the right by a certain number of bits, add 0 to the left for positive numbers, 1 for negative numbers to the left, and discard the right side |
x = 20 y = x | 2 z = y >> 2 n = ~0
Assignment operator
| Operator | Description |
|---|---|
= | Assignment, returns the value of the expression on the right. |
+= | Calculates and writes back as binary +, supports numerical addition and string concatenation. |
-= | Compute as binary - and write back. |
*= | Compute as binary * and write back. |
/= | Calculates and writes back the binary /, the result is a floating point number. |
%= | Compute as binary % and write back. |
<<= | Calculated as binary << and written back. |
>>= | Compute as binary >> and write back. |
&= | Compute as binary & and write back. |
^= | Compute as binary ^ and write back. |
|= | Calculated as binary | and written back. |
Syntax
target = value target += value target -= value
Parameters
| Parameters | Type | Is it required | Default value | Valid range or optional value | Meaning |
|---|---|---|---|---|---|
target | Variable/Array/Object | Yes | None | Variable variables, array numeric subscripts, ordinary object fields, class instance fields | A writable target that receives write-back results. |
value | Any | Yes | None | Determined by the corresponding binary operator | The rvalue of an ordinary assignment, or the right operand of a compound assignment. |
Property chains and dynamic subscripts can be targeted, such as config.title, items[index], config.article.id. Object expressions and subscript expressions in the target are evaluated only once; the execution order is fixed as "resolve target reference → read current value (compound assignment) → evaluate rvalue → operation → write back to the same target".
Return Value
- Normal
=returns the value of the expression on the right. - Compound assignment returns the new value after writing back.
- Variables, local variables, parameters and closure capture variables inherit their respective scopes; compound assignment cannot modify constants.
config = {title: 'BT', article: {id: 0}} items = [8] title = (config.title += ' Lang') id = (config.article.id += 1) shifted = (items[0] <<= 2) // Output: BT Lang print title // Output: 1 print id // Output: 32 print shifted
Increase and decrease
Function and syntax
| operator | return value | write back result |
|---|---|---|
target++ | Old value before modification | Current value plus 1. |
target-- | Old value before modification | Current value minus 1. |
++target | New modified value | Current value plus 1. |
--target | Modified new value | Current value minus 1. |
target must be a writable variable, array numeric subscript, ordinary object field or class instance field, and the current value can only be Int or Float. Strings, Boolean values, null, empty, and other objects cannot increment or decrement.
i = 1 old = i++ current = ++i // Output: 1 print old // Output: 3 print current
Conditional operator
? : Returns one of two expressions based on a condition. When the right side of : is omitted, empty is returned if the condition is not met.
x = 10 y = x == 10 ? 1 : 2 z = x == 9 ? 7
Members and calls
| Operator | Description |
|---|---|
() | Change the order of operations or call a function. |
[] | Access array subscripts, string characters, or object dynamic fields. |
. | Access object fields or prototype methods. |
// Output: H print ['hello'][0].to_uppercase()
Notes
-
&&,||and??are all short-circuit evaluations. When the result can be determined on the left side, the function call, field access or other expression on the right side will not be executed. -
??only handlesnullandempty, and will not treat0,false, and empty strings as missing values.
|| uses BT truth rules, null, empty, 0, false, empty strings, empty arrays, and empty objects are considered false.
- Ordinary assignment, compound assignment and auto-increment and auto-decrement will fix the object expression and dynamic subscript to the same target reference, and will not call the function in the target repeatedly due to writeback.
BT / Math static objects, extension objects, and class definition objects are read-only; assignment to these targets throws an error.
- Non-lvalues such as
1 += 2,foo()++, and(a + b) += 1will report errors at compile time, and constants cannot use compound assignment or auto-increment and self-decrement.
**=, &&=, ||=, ??= operators; they should be explicitly written using existing ordinary assignment and short-circuiting expressions when needed.