Expression
Makes javascript expressions available in the Bubble page, and the result available to other Bubble elements.
Place the element on the page, ensuring it is "visible" so it can load its properties.
It can be resized to very small to keep out of the way.
Setting properties
Expression
Will be evaluated as javascript. Can include dynamic data.
Result type
usually you will want a "vanilla" type such as text, number, date, yes/no. Can return Bubble data types, API return types.
Result is a list
to produce list of texts, for example.
Yes/no result
In javascript, the equivalent of yes/no is true/false.
true; // results in yes
Runs on every change
Bubble will re-run the expression if any of the dynamic data is updated.
When the Expression element is rendered, dynamic data is often set to empty at first. In this case, Expression runs at a minimum of two times.
Inside a repeating group
Place an Expression inside a repeating group cell and the following dynamic data become available:
Current cell's index
the position within the RG.Current cell's number/text/etc
the RG data type.
Possible empty dynamic data
The value of the expression gets evaluated as javascript. If empty it shouldn't get evaluated.
The expression needs to make sense even if parts of it are empty. A dynamic expression returning empty will have a blank area in the expression, which can be okay, or not, depending on what else is there.
Example of problems and fixes
// problem
Search for Products:first item's Price * 6
Search finds price 7
, Expression becomes 7 * 6
with result 42
Search finds no products, Expression becomes * 6
with resulting error
// fix
(Search for Products:first item's Price + 0) * 6
Search finds price 7
, Expression becomes (7 + 0) * 6
with result 42
Search finds no products, Expression becomes ( + 0) * 6
with result 0
Conditional set to empty
Instead of making fixes to the expression to allow for empty dynamic data, there is the option of making the entire Expression empty with the Conditional tab.
Localization
When dynamic data is included in the script, Bubble automatically converts it to text according to local language. Instead of depending on the automatic conversion, it is better to explicitly convert to text with Bubble's :format as text
.
This can be used to convert yes/no data to true/false.
Also to convert numbers to text while choosing .
as a decimal separator, and no thousand separator.
This makes the resulting script more compatible with javascript.