All Arcade expressions must return a value. The permissible data types returned from an Arcade expression are defined by the profile. There are two types of return statements: explicit returns and implicit returns.
Explicit returns
An explicit return is always preceded with the return
keyword. This can be used for single-line expressions...
return "Hello world";
return ( ($feature.POP_2010 - $feature.POP_2000) / $feature.POP_2000) * 100;
...and multi-line expressions.
var total = 5 + 10;
return total;
var myArray = [10,20,30,40,50,60,70];
var total = 0;
for(var k in myArray){
total += myArray[k];
}
return total;
It can also be used when defining functions.
function calculateRatio(values, index){
var value = values[index];
return value / Sum(values);
}
Implicit returns
An implicit return returns the last executable statement of an expression. This is true regardless of an expression's length.
5 + 10;
// returns 15
($feature.POP_2010 / $feature.POP_2000) * 100;
Although the return
keyword is not used in these multi-line expressions, the value of total
is still returned.
var total = 5 + 10;
total;
// returns 15
var myArray = [10,20,30,40,50,60,70];
var total = 0;
for(var k in myArray){
total += myArray[k];
}
total;
// returns 280
User-defined functions can also be written with an implicit return.
function calculateRatio(values, index){
var value = values[index];
// the value computed in the final statement is returned by the function
value / Sum(values);
}
Guidelines
Expressions with explicit returns benefit from providing clarity to anyone reading it. When multi-line expressions lack an explicit return, it can be easily interpreted as incomplete or unfinished. The following are suggested guidelines when authoring expressions.
- Always use explicit returns when defining custom functions.
- Use implicit returns in single-line expressions.
- Always use explicit returns in multi-line expressions.