Master ArcGIS Online Arcade: If Statement Expression Techniques

By Katherine

ArcGIS Online, a cloud-based platform provided by Esri, incorporates Arcade, a language that enables the creation of sophisticated expressions and functions within the platform. The 'if' statement, a fundamental control flow structure in programming, is exceedingly vital in Arcade for applying conditional logic in your expressions. Let's delve into how to utilize the 'if' statement within ArcGIS Online Arcade expressions.

Arcade
Arcade

The 'if' statement in Arcade is designed to execute a specific action based on whether a certain condition is true or false. It follows the syntax: if (expression) {}. Here, the 'if' keyword is followed by the condition or expression in parentheses, and the action or code block that should be executed if the condition is true is defined within braces {}.

an arcade with neon lights and pool tables
an arcade with neon lights and pool tables

The Basic 'if' Statement

The most elementary form of an 'if' statement tests a condition and performs an action when that condition is true. Here's a simple example:

an old computer game with hearts and numbers on the screen, which reads score ranking
an old computer game with hearts and numbers on the screen, which reads score ranking

```arcade if (age >= 18) { return 'Adult'; } ```

In this snippet, the word 'Adult' will be returned if the value of the 'age' field is 18 or older.

The corresponding code will stop executing if the condition is false and move on to the next expression. More often than not, however, you might want to execute a different action when the condition is false. This is where the 'else' statement comes in.

an image of a computer screen with the word arcade on it
an image of a computer screen with the word arcade on it

The 'if-else' Statement

The 'if-else' constructs in Arcade augment the 'if' statement by enabling you to specify actions for both true and false outcomes. It follows this syntax: if (expression) { actionIfTrue; } else { actionIfFalse; }. An example could be:

```arcade if (age >= 18) { return 'Adult'; } else { return 'Minor'; } ```

In this scenario, the function will return 'Adult' if the 'age' field is 18 or above, and 'Minor' if it's below 18.

Arcade Website | Unique Web Designs | Gamer Designs | Arcade Aesthetic
Arcade Website | Unique Web Designs | Gamer Designs | Arcade Aesthetic

Nesting 'if-else' Statements

'if-else' statements in Arcade can be nested to create complex conditional logic. This is done by placing one 'if-else' statement inside another, like a set of Russian dolls. Here's an illustration:

```arcade if (age >= 18) { if (isStudent) { return 'College Student'; } else { return 'Non-Student Adult'; } } else { return 'Minor'; } ```

In this example, the function first checks if the person's age is 18 or older. If the person is an adult, it performs another 'if-else' check to determine if the person is a student. If the person is a student, the function returns 'College Student'. If the person is not a student, it returns 'Non-Student Adult'. If the person is younger than 18, the function simply returns 'Minor'.

MakeCode Arcade Convert a picture into a sprite or background; Pedersen
MakeCode Arcade Convert a picture into a sprite or background; Pedersen

Using 'else if' for Multiple Conditions

When you need to test a condition against multiple possible values, 'else if' can be used to skip to the next condition if the previous one doesn't match. For example:

Arcade date
Arcade date
Your daily dose of brainrot just got pixelated and put in an arcade 🕹️✨Wanna play?
Your daily dose of brainrot just got pixelated and put in an arcade 🕹️✨Wanna play?
Arcade First examination inspo pubmat #yona
Arcade First examination inspo pubmat #yona
Arcade date
Arcade date
edward 😕😕
edward 😕😕
Arcade Videogames Festival App - UX/UI iOS Design - Angel Gonzalez
Arcade Videogames Festival App - UX/UI iOS Design - Angel Gonzalez
a woman standing in front of some pinball machines
a woman standing in front of some pinball machines
Arcade Font
Arcade Font
the arcade is brightly lit up at night
the arcade is brightly lit up at night
Arcade poster👾🕹️ (made by me)
Arcade poster👾🕹️ (made by me)

```arcade if (dayOfWeek == 'Monday') { return 'Start of the week'; } else if (dayOfWeek == 'Friday') { return 'End of the week'; } else if (dayOfWeek == 'Saturday' || dayOfWeek == 'Sunday') { return 'Weekend'; } else { return 'Midweek'; } ```

In this snippet, the function will jump straight to the next condition if the current one isn't met, up until it reaches a suitable category for the 'dayOfWeek' value. The 'else' at the end will catch all other days that weren't explicitly mentioned.

Understanding how to implement 'if', 'else if', and 'else' statements in Arcade within ArcGIS Online is crucial for developing intricate expressions that can adapt to and interact with your data's nuances. By leveraging these control flow structures, you can create powerful, dynamic, and informative expressions that truly enhance your mapping and visualization experiences.

Now that you're well-versed with the use of 'if' statements, why not explore more advanced aspects of Arcade to further expand your expression-creation capabilities? The path to mastering Arcade is filled with exciting opportunities for learning and innovation, so take your time, indulge in your curiosity, and unlock the full potential of your data and maps.