D
D
Developers
Search…
Let / Const / Set / Global
Pulsar comes with full support for scoped variables exactly as you know them from other languages. This means you can define a variable within a specific scope, mutate it (or create one that is immutable), and read its value.
Similar to dynamically typed languages, variables are not constrained to specific data types, and as such, there are no type definitions when you declare them.
Variable flows don't define a body, and shorthand flow syntax is recommended.

Let

let flow allows you to define a variable within the current scope:
1
{* Declaring variable *}
2
{[ let dogName = "Rex" /]}
3
{{ dogName }} // Rex
Copied!
Variables declared with let are mutable, meaning you can change their value inside the same scope they were defined at. When you try to re-define a variable that was already defined, an error is thrown.

Const

const flow allows you to define a variable within the current scope:
1
{* Declaring variable *}
2
{[ const dogName = "Rex" /]}
3
{{ dogName }} // Rex
Copied!
Variables declared with const are immutable, meaning you can't change their value inside the same scope they were defined at. When you try to re-define a variable that was already defined, an error is thrown. When you try to mutate this variable, an error is thrown as well.

Set

set flow allows you to modify a variable defined using let flow:
1
{* Declaring variable *}
2
{[ let dogName = "Rex" /]}
3
{{ dogName }} // Rex
4
5
{* Mutating variable *}
6
{[ set dogName = "T-Rex" /]}
7
{{ dogName }} // T-Rex
Copied!
Only variables declared as mutable (let) can be mutated, otherwise, an error is thrown. Also, set expects variable to be already defined in the current scope, and it can't define new variables.
You can also use shorthand set as you would in other languages:
1
{* Declaring variable *}
2
{[ let dogName = "Rex" /]}
3
{{ dogName }} // Rex
4
5
{* Mutating variable *}
6
{[ dogName = "T-Rex" /]} // Note the missing "set"
7
{{ dogName }} // T-Rex
Copied!

Global

Variables defined with global flow are always defined at the top-most scope, regardless of the scope used to execute this definition. This makes them available to any executed code as soon as they are defined:
1
{[ if true ]}
2
{[ global variableInsideBody = "Text" /]}
3
{{ variableInsideBody }} // Properly executes
4
{[/]}
5
6
{{ variableInsideBody }} // Also properly executes
Copied!
Variables declared with global are immutable. The purpose behind this flow is to use it for variables that are needed throughout a blueprint, everywhere, and are constants. A good use for global flow are configuration values:
1
{[ global mySettings = "1.0" /]}
2
{[ global myOtherSettings = "abcdefgh" /]}
Copied!
When defined, you can use them anywhere from that point onwards.
Global variables are not the same as in some languages where they are available to all scripts that run. In Pulsar, globals are only available within a specific blueprint that defined them. If you want to define variables that span the entire execution, refer to our javascript guide to learn how to define a custom payload.

Scopes

Variables (with the exception of those defined using global) are always created within the executed scope. Take the following example:
1
{[ if trueOrFalse ]}
2
{[ const variableInsideBody = "Text" /]}
3
{{ variableInsideBody }} // Properly executes
4
{[/]}
5
6
{{ variableInsideBody }} // Throws error
Copied!
Since variables are scoped (and for flow creates a new variable within its body/scope), the substitution will fail because the variable was defined inside another body, out of scope.
Parent/child scopes are supported as well. Take this example:
1
{[ if trueOrFalse ]}
2
{[ const variableInsideBody = "Text" /]}
3
{{ variableInsideBody }} // Properly executes
4
{[ for number in numbers ]}
5
{{ variableInsideBody }} // Properly executes as well
6
{[/]}
7
{[/]}
8
9
{{ variableInsideBody }} // Throws error
Copied!
Because for loop is inside the scope that was used to define variableInsideBody , we can use any property that was defined in parent scopes as well.
Null support
Null or empty values are handled gracefully, but most transformers consider them an error. You can check for whether a value is null by using @isNull(variable) function.
Last modified 7mo ago
Copy link