Debugging

Arcade expressions may be authored in any text editor. ArcGIS Pro and ArcGIS Online have expression editors that provide help for debugging expressions. You can also use the playground for testing Arcade expressions.

Playground

The playground provides an integrated text editor configured to a specific profile. For example, when authoring an expression for a popup, the playground will provide test values for each profile variable, and list all the available functions in the profile.

As you type, the playground will validate the syntax of the expression.

Console

The Console function allows you to log messages and values to the console of the playground or expression editor in ArcGIS Online or ArcGIS Pro. When writing expressions in web apps using the ArcGIS API for JavaScript, this function also logs messages in the browser's console window.

Use dark colors for code blocksCopy
    
1
2
3
4
var i = 0;
i++;
Console(i);  // logs `1` in the console
return ++i;  // returns `2` from the expression

This function is useful when debugging expressions that define complex functions or iterate through items in a loop. It can also be useful for checking the values of profile variables.

Use dark colors for code blocksCopy
             
1
2
3
4
5
6
7
8
9
10
11
12
13
var total = 0;
for(var att in $feature){
  total += feature[att];
  console("attribute:", att);
  console("value:", feature[att]);
  console("total:", total);
}
return total;

// Console will log the following for each iteration in the loop:
// attribute: "Population"
// value: 1000
// total: 50000

This can help you identify where a loop or a statement in a function fails.

Comments

When a expression throws an error, you can use comments to disable statements line-by-line to understand where the expression behaves unexpectedly. Because this can be time consuming, it is often used as a last-resort technique.

Use dark colors for code blocksCopy
                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var dogs = $feature["dogs"];
var cats = $feature["cats"];
var rats = $feature["rats"];
var turtles = $feature["turtles"];
var birds = $feature["birds"];

var groups = [
  dogs,
  cats,
  rats,
  turtles,
  birds,
];

function simpsonsDiversityIndex(vals){
  var k = Array(Count(vals));
  var t = sum(vals);
  return t;

  // disable the code below to see if the sum of
  // the array's values is returning an unexpected result

  // for(var i in vals){
  //   var n = vals[i];
  //   k[i] = n * (n-1);
  // }
  // var s = Sum(k);
  // var di = 1 - ( s  / ( t * (t-1) ) );
  // return Round(di*100);
}

simpsonsDiversityIndex(groups);

Replace profile variables

It's a good idea to test your expression with various input values, such as large numbers, small numbers, negative numbers, all zeros, all nulls, etc. This helps you write a robust expression.

Assigning profile variables to your own descriptive variable names helps you not only write a more readable expression, but it also helps when testing various combinations of values. The following example shows an abbreviated expression to conserve space.

Use dark colors for code blocksCopy
          
1
2
3
4
5
6
7
8
9
10
// the field names here aren't descriptive. Assigning them
// to descriptive field names makes it easier to write the expression
var dogs = $feature["ANCOD_001"];
var cats = $feature["ANCOD_002"];
var rats = $feature["ANCOD_003"];
var turtles = $feature["ANCOD_004"];
var birds = $feature["ANCOD_005"];

// expression continues and uses the variables
// ...

To test various scenarios you can do something like this:

Use dark colors for code blocksCopy
              
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var testValues = [100,132,1234,0,123];  // returns  68.3 GOOD!
// var testValues = [100,0,0,0,0];  // returns 0 GOOD!
// var testValues = [0,0,0,0,0];  // returns 0 GOOD!
// var testValues = [100,132,1234,0,null];  // throws error - FIX THIS!
// var testValues = [null,null,null,null,null];  // throws error - FIX THIS!

var dogs = testValues[0]  // $feature["ANCOD_001"];
var cats = testValues[1]  // $feature["ANCOD_002"];
var rats = testValues[2]  // $feature["ANCOD_003"];
var turtles = testValues[3]  // $feature["ANCOD_004"];
var birds = testValues[4]  // $feature["ANCOD_005"];

// expression continues and uses the variables
// ...

If you get the expected results for one set of test values, simply disable that line and enable the next line of test values, annotating the results as you test.

Use dark colors for code blocksCopy
              
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// var testValues = [100,132,1234,0,123];  // returns  68.3 GOOD!
var testValues = [100,0,0,0,0];  // returns 0 GOOD!
// var testValues = [0,0,0,0,0];
// var testValues = [100,132,1234,0,null];
// var testValues = [null,null,null,null,null];

var dogs = testValues[0]  // $feature["ANCOD_001"];
var cats = testValues[1]  // $feature["ANCOD_002"];
var rats = testValues[2]  // $feature["ANCOD_003"];
var turtles = testValues[3]  // $feature["ANCOD_004"];
var birds = testValues[4]  // $feature["ANCOD_005"];

// expression continues and uses the variables
// ...

Continue this process for each line of test values. By the end of your tests, you will have results that look something like this:

Use dark colors for code blocksCopy
              
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// var testValues = [100,132,1234,0,123];  // returns  68.3 GOOD!
// var testValues = [100,0,0,0,0];  // returns 0 GOOD!
// var testValues = [0,0,0,0,0];  // returns 0 GOOD!
// var testValues = [100,132,1234,0,null];  // throws error - FIX THIS!
var testValues = [null,null,null,null,null];  // throws error - FIX THIS!

var dogs = testValues[0]  // $feature["ANCOD_001"];
var cats = testValues[1]  // $feature["ANCOD_002"];
var rats = testValues[2]  // $feature["ANCOD_003"];
var turtles = testValues[3]  // $feature["ANCOD_004"];
var birds = testValues[4]  // $feature["ANCOD_005"];

// expression continues and uses the variables
// ...

After fixing the broken scenarios, simply remove the comments and replace the test values with the profile variables.

Use dark colors for code blocksCopy
        
1
2
3
4
5
6
7
8
var dogs = $feature["ANCOD_001"];
var cats = $feature["ANCOD_002"];
var rats = $feature["ANCOD_003"];
var turtles = $feature["ANCOD_004"];
var birds = $feature["ANCOD_005"];

// expression continues and uses the variables
// ...

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.