Variables store data values. They are declared with the var keyword and may be assigned any value. Variables must be declared, and identified with a name. The identifier must adhere to the following rules:
begins with a letter,
contains only letters, numbers, or underscore, and
As a guideline, variable names should not begin with $ as that pattern is reserved for profile variables. Variable names are not case-sensitive.
Declaring variables
A variable can be declared with a literal (or fixed) value.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
// variable as number literalvar x = 10;
// variable as text literalvar name = "Dave";
// variable as date literalvar startDate = Date(2015,0,1);
// variable as boolean literalvar likesArcade = true;
Variables can be declared with expressions. In this scenario, the variable stores the computed value.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
var total = 100 + 50;
// the value of total is 150var first = "Paul";
var last = "Richard";
var fullName = first + " " + last;
// the value of fullName is "Paul Richard"
You can declare variables with no value, and assign their value later in the expression.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
var x; var y;
x = 100;
y = 50;
return x+y;
// output is 150;
Value reassignment
Variables may be reassigned to new values at any point in the expression;
Use dark colors for code blocksCopy
1
2
3
4
5
var age = 35;
var birthday = Date(1980, 5, 2);
// computes the age based on the time the expression was executedage = Round( DateDiff(Now(), birthday, "years") );
Variables can be re-assigned new values of different types.
Use dark colors for code blocksCopy
1
2
3
4
5
// declared as a numbervar age = 35;
// reassigned as textage = "35 years";
Remember, variables must be declared. The following will result in a compilation error.
Use dark colors for code blocksCopy
1
2
age = 35;
// age has not been declared with var
Profile variables
Profile variables are data values provided as input to every Arcade expression written in a given profile. Profile variables always begin with the $ character. The profile variables available to an expression depend on the profile specification.
For example, the $feature profile variable represents a feature - its attributes and geometry - which may be used in an expression. This is available in most profiles as a way to calculate values from a feature's attributes.
Dot notation
You can access feature attributes using dot notation: (e.g. $feature.fieldName).
Use dark colors for code blocksCopy
1
2
3
4
5
// returns % of population with a college degreevar percentCollege = Round(($feature.COLLEGE / $feature.POPULATION) * 100, 2);
// returns the population density per square kilometervar popDensity = Round( $feature.POPULATION / AreaGeodetic(Geometry($feature), "square-kilometers") );
Bracket notation
Attribute and dictionary values can also be referenced using square brackets.
Use dark colors for code blocksCopy
1
2
// returns % of population with a college degreeRound(($feature["COLLEGE"] / $feature["POPULATION"]) * 100, 2);
The square bracket notation must be used when referencing any of the following:
values from joined tables (e.g. $feature["joinKey.fieldName"])
unusual field names, such as non-Latin characters outside the Unicode BMP
field names using variables
Use dark colors for code blocksCopy
1
2
3
// returns % change in votes from 2012 to 2016// The data referenced in this expression comes from a table joinRound((($feature["COUNTY_ID.VOTED_DEM_2016"] - $feature["COUNTY_ID.VOTED_DEM_2012"]) / $feature["COUNTY_ID.VOTED_DEM_2012"]) * 100, 2);
You should explicitly list all field attributes that will be used at the top of the expression. This will ensure the profile will request the appropriate data from required fields so the expression evaluates properly.
Expects combined with square bracket notation is required when referencing attributes using variables.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Expects($feature, "POPULATION_2000", "POPULATION_2005", "POPULATION_2010", "POPULATION_2015");
var y = 2000;
var prefix = "POPULATION_";
var changes = [];
for (var y = 2000; y < 2020; y += 5){
// attribute names dynamically built in loop must be// declared in Expects to ensure it is downloaded// at the time of executionvar current = $feature[prefix + y];
var next = $feature[prefix + (y + 5)];
var change = (next - current) / current;
Push(changes, change);
}
returnAverage(changes);
Scope
Scope determines whether a variable is accessible in certain parts of the expression.
Global scope
Variables declared outside a function have global scope through the expression. That means you can use a variable anywhere in the expression, inside or outside functions and blocks as long as it is after the variable is declared.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
var baseValue = 10; // this var has global scopefunctionisGreater (x) {
return x > baseValue; // it can be used inside a function}
Console(baseValue) // and referenced outside a functionreturn isGreater(100);
Local scope
Variables declared inside a function have local scope. They can only be used inside the function definition.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
var baseValue = 10; // this var has global scope// x has local scopefunctionisGreater (x) {
// it can only be used inside the functionreturn x > baseValue;
}
Console(x); // this will throw an error;return isGreater(100);
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
var baseValue = 10; // this var has global scopefunctionisGreater (x) {
var factor = 2; // factor and x have local scopereturn (x * factor) > baseValue;
}
return isGreater(100);
Variables declared inside functions can override global scope variables.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var myGlobal = 1;
functionuseGlobal() {
var n = 10; // n has local scopereturn myGlobal * n; // will return the globally defined variable}
functionoverrideGlobal() {
var myGlobal = 1000;
return myGlobal * 10; // will use the locally defined myGlobal variable}
var z = overrideGlobal(); // returns 10000var k = useGlobal(); // returns 10return z + k; // returns 10010
Block scope
Arcade does not support block scope. This means variables defined inside a control block (i.e. if or for) will be available outside the block.
Use dark colors for code blocksCopy
1
2
3
4
5
var myGlobal = 1;
for(var k=1; k<100; k++) {
var n = 10; // Defined in block, still available outside the block.}
return k + n; // returns 110