An expression is a list of instructions that evaluate to a value. Each instruction in the expression is also known as a statement. Statements are preceded with a keyword or identifier indicating a function name or variable name. Statements typically end with a semi-colon.
Single-line expressions
Single-line expressions usually consist of one statement. Because there is only one statement, the use of an explicit return and semi-colon is not necessary.
"Hello world" // returns "Hello world"
Multiple statements on one line
You may write multiple statements on a single line. In this scenario, each statement must be separated with a semi-colon. This pattern is more common with variable declarations.
var x = 3; var y = 4;
return x + y;
// returns 7
We suggest you avoid returning values on lines that contain multiple statements. While the following example is legal syntax, it is hard to read.
var x = 3; var y = 4; return x+y;
// returns 7
Multi-line expressions
Multi-line expressions consist of multiple statements, including variable declarations, function definitions, and invoking functions. Because multi-line expressions can be complex, we suggest you use semi-colons to mark the end of statements and explicit returns to indicate the end of a script.
function calculateRatio(values, index){
var value = values[index];
return value / Sum(values);
}
var values = [ $feature.CategoryA, $feature.CategoryB, $feature.CategoryC, ];
var selectedIndex = 1;
var ratio = calculateRatio(values, selectedIndex);
return ratio;
One statement on multiple lines
Some statements can become very long because of long variable names, multiple chained functions, functions with a long list of parameters, and long conditional statements. In these scenarios, you should break up the statement across multiple lines to make it easier to read.
Chained functions
Because Arcade allows you to chain functions, a single statement can become quite long.
// multiple functions in one statement chained on one line
AreaGeodetic(Intersects(Filter(FeatureSetByName($map, "public lands", ["class"], true), "class = 'sensitive'"), $feature), "square-kilometers");
Break up the statement across multiple lines to make it easier to read.
// multiple functions in one statement chained on multiple lines
AreaGeodetic(
Intersects(
Filter(
FeatureSetByName($map, "public lands", ["class"], true),
"class = 'sensitive'"
),
$feature
),
"square-kilometers"
);
In the case of FeatureSet functions, it helps to assign the FeatureSet to a variable.
var publicLands = FeatureSetByName($map, "public lands", ["class"], true);
AreaGeodetic(
Intersects(
Filter( publicLands, "class = 'sensitive'"),
$feature
),
"square-kilometers"
);
Long conditional statements
Some conditional statements can be complex.
if((score == "High" || value > 90) && (cases > 0 && status == "active")){
// statements execute in this block if the condition is true
}
Break the conditional statement onto multiple lines to help readability.
if(
(score == "High" || value > 90) &&
(cases > 0) &&
(status == "active")
){
// statements execute in this block if the condition is true
}
See if - else and bitwise operators for more information about conditional statements.
Guidelines
- Keep one statement per line. It makes the script easier to read.
- Long statements should be broken up across multiple lines. Line breaks should occur in the following locations:
- In a set of chained functions, at the start of invoking a function.
- In functions with a long list of parameters, or a long value, at the start of a parameter.
- Long conditional statements should be broken up after an operator is used.
Keywords
A keyword is a reserved word in the Arcade language. These words indicate an instruction to execute in the Arcade engine.
The following are keywords in Arcade.
Keyword | Instruction |
---|---|
break | Breaks or stops the execution of statements in a for loop. See break for more information. |
continue | Continues the execution of statements in a for loop. See continue for more information. |
else | Indicates the start of a block of statements to execute when a condition defined by a preceding if evaluates to false. See else for more information. |
for | Indicates the start of a block of statements to execute iteratively based on an iterator or conditional statement. See for for more information. |
function | Defines a function. See functions for more information. |
if | Indicates the start of a block of statements to execute when a condition defined in parentheses evaluates to true. See if for more information. |
in | Preceded by for and a variable declaration. This instructs the statement to iterate through all items in a FeatureSet or Array, or properties in a Dictionary or Feature. See for...in for more information. |
return | Indicates the value to return from a function or a script. See return for more information. |
var | Declares a variable. See variables for more information. |
while | Indicates the start of a block of statements to execute iteratively based on a conditional statement. See while for more information. |