If - else

If

The if keyword defines a block of statements to execute if a condition evaluates to true. The condition must evaluate to either true or false. Otherwise, the expression will fail with an execution error. Conditional statements require the use of one or more comparison operators.

Use dark colors for code blocksCopy
1
2
3
4
5
var x = 10;

if(x > 5){
  return x + " is greater than 5";
}

Logical operators are used to group multiple conditions.

Use dark colors for code blocksCopy
1
2
3
4
5
6
var x = 10;
var y = 0;

if(x > 5 || y < 0){
  return "This text will be returned if x is greater than 5 OR if y is less than 0";
}

Else

The else keyword defines a block of statements to execute if a condition defined in a preceding if statement evaluates to false.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
var x = 10;

if(x > 5){
  return x + " is greater than 5";
} else {
  return x + " is less than 5 or equal to 5";
}

The Boolean function casts values to either true or false. For example, 0 will cast to false and any other number will cast to true.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function isOdd(value){
  // Returns either 1 or 0.
  // 1 = odd number
  // 0 = even number
  // Boolean() converts the raw value to a boolean
  if ( Boolean(value % 2) ) {
    return "Yes. This is odd."
  }
  else {
    return "Nope. Not odd.";
  }
}

isOdd(213);  // returns "Yes. This is odd."
isOdd(2);  // returns "Nope. Not odd."

Nested if/else

You can nest if/else blocks within each other.

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
var x = 10;

if(x > 5){
  return x + " is greater than 5";
} else {
  x *= 2;

  if(x > 5){
    return x + " is now greater than 5";
  } else {
    x *= 2;

    if(x > 5){
      return x + " is FINALLY greater than 5";
    } else {
      return x + " is still less than 5";
    }
  }
}

Logical functions

Arcade provides several convenient logical functions that make working with assigning variables or returning values based on conditions easier to read in a condensed format.

IIF

The iif() function returns a value if a condition evaluates to true, and a default value to return if the condition evaluates to false.

Use dark colors for code blocksCopy
1
2
3
4
5
var a = AreaGeodetic($feature, 'square-miles');
var x = IIF(a >= 100, "Warning", "Watch");
return x;
// if a is 101, then x = "Warning";
// if a = 99, then x = "Watch";

Decode

The Decode() function returns a value if it matches the value of a variable.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
// Returns a number matching a storm category description of a text value
Decode( $feature.CATEGORY,
  "Category 5", 5,
  "Category 4", 4,
  "Category 3", 3,
  "Category 2", 2,
  "Category 1", 1,
  0
);
// if CATEGORY = "Category 2", then the expression returns 2.
// if CATEGORY = "Tropical Storm", then the expression returns 0.
// if CATEGORY = "Category 4", then the expression returns 4.

DefaultValue

DefaultValue() returns the value of the second parameter if the value of the first parameter is null or an empty text (i.e. '').

Use dark colors for code blocksCopy
1
2
DefaultValue($feature.fieldName, "No data");
// returns "No data" if fieldName is null or empty

IsEmpty

IsEmpty() returns true if the provided value is null or an empty text (i.e. '').

Use dark colors for code blocksCopy
1
2
IsEmpty($feature.fieldName);
// returns true if fieldName is null

IsNan

IsNan() returns true if the value is not a number.

Use dark colors for code blocksCopy
1
2
IsNan(1/0);
// returns true

When

The When() function allows you to define a series of values to return if a matching condition evaluates to true. In the scenario where multiple conditions evaluate to true, then the value associated with the first true condition is returned.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
// Returns storm category of hurricane force winds
return When(
  $feature.WIND_SPEED >= 157, "Category 5",
  $feature.WIND_SPEED >= 130, "Category 4",
  $feature.WIND_SPEED >= 111, "Category 3",
  $feature.WIND_SPEED >= 96, "Category 2",
  $feature.WIND_SPEED >= 74, "Category 1",
  "Tropical Storm"
);
// if WIND_SPEED = 100, then the expression returns "Category 2".
// if WIND_SPEED = 50, then the expression returns "Tropical Storm".
// if WIND_SPEED = 130, then the expression returns "Category 4".

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