Type casting
Arcade allows you to cast a value of one data type to another type. You can do this explicitly using any of the casting data functions, or allow Arcade to handle casting implicitly. The following are the implicit and explicit type conversion rules in Arcade.
Explicit casting
Explicit casting refers to the practice of using a function to convert a value of one data type to another. The expression author is intentionally making a decision to cast the type. These are the casing functions:
Explicit number conversion
Number(value, format?)
The Number function converts a date or a text value to a number.
Input Type | Output Type | Result |
---|---|---|
Boolean | Number | Value of true casts to 1 ; false casts to 0 . |
Date | Number | Number of milliseconds since Jan. 1, 1970 (i.e. the Unix epoch). |
Text | Number | Behaves as an implicit cast if no format is provided. If format is specified but not matched, then returns N . |
Explicit boolean conversion
Boolean(value)
The Boolean function converts a number or text value to a boolean. Any number, except zero, will return true
. Only the text "true"
will return true
. All other scenarios return false
.
Explicit date conversion
Date(value, format?)
The Date function converts a number or text value to a date.
Input Type | Output Type | Result |
---|---|---|
Number | Date | Creates a new date based on the number of milliseconds since Jan. 1, 1970. |
Text | Date | Creates a new date if text is ISO 8601. Otherwise, the date value will depend on format text. Returns null if no there's no match. |
Explicit text conversion
Text(value, format?)
The Text function converts any value to text.
Input Type | Output Type | Result |
---|---|---|
Number | Text | Number formatted according to format text. |
Date | Text | ISO 8601 date format, or formatted according to format text. |
Feature | Text | JSON representation of the feature. |
Dictionary | Text | JSON representation of the dictionary. |
Geometry | Text | JSON representation of the geometry. |
Array | Text | See Concatenate function. |
Implicit casting
Implicit casting occurs when the Arcade language automatically casts data values from one type to another on behalf of the script author. This can happen in logical statements, function parameters, and values returned from expressions.
The following are the rules for implicit type casting in Arcade.
Implicit text conversion
Input Type | Output Type | Result |
---|---|---|
Number | Text | Decimal representation of the number. |
Date | Text | ISO 8601 date format. |
Null | Text | Empty text (e.g. '' ). |
Boolean | Text | 'true' or 'false' . |
Array | Text | JSON representation of the array as text. Use See Concatenate function. |
Feature | Text | JSON representation of the feature as text. |
Dictionary | Text | JSON representation of the dictionary as text. |
Geometry | Text | JSON representation of the geometry as text. |
Implicit number conversion
Input Type | Output Type | Result |
---|---|---|
Date | Number | Number of milliseconds since Jan. 1, 1970 (i.e. the Unix epoch). |
Boolean | Number | 0 or 1 |
Null | Number | 0 |
Text | Number | If decimal text, then converted to decimal number. Empty text or spaces are cast to 0 . Other values are converted to N . |
Array | Number | N |
Feature | Number | N |
Dictionary | Number | N |
Geometry | Number | N |
Implicit boolean conversion
Input Type | Output Type | Result |
---|---|---|
Date | Boolean | false |
Number | Boolean | true , unless the number is 0 , which casts to false . |
Null | Boolean | false |
NaN | Boolean | false |
Text | Boolean | If lower case text value of 'true' then casts to true . All other text casts to false . |
Array | Boolean | false |
Feature | Boolean | false |
Dictionary | Boolean | false |
Geometry | Boolean | false |
Implicit date conversion
Input Type | Output Type | Result |
---|---|---|
Number | Date | null |
Text | Date | Date if Text is ISO 8601. Otherwise null |
Null | Date | null |
NaN | Date | null |
Boolean | Date | null |
Array | Date | null |
Feature | Date | null |
Dictionary | Date | null |
Geometry | Date | null |
Logical statements
See Logical operators to learn how to use logical operators.
Operator | Behavior |
---|---|
&& | Left and right operands must be boolean. There is no implicit type conversion. |
\|\| | Left and right operands must be boolean. There is no implicit type conversion. |
! | Argument must be boolean. There is no implicit type conversion. |
Comparison statements
See Comparison operators to learn how to use logical operators.
Operator | Behavior |
---|---|
!= | There is no implicit type conversion for this operator. The values must not be equal. Text, dates, numbers, and booleans are treated as value types. Features, dictionaries, and geometries are treated as objects. |
== | There is no implicit type conversion for this operator. The values must be equal. Text, dates, numbers, and booleans are treated as value types. Features, dictionaries, and geometries are treated as objects. |
< , > , <= , >= | Object types (i.e. Arrays, Features, Dictionaries, Geometry) will always return false , unless checking equality (e.g. == ). In this case, true will be returned if they are the same object. If both operands are different types, then they will implicitly be converted to numbers. |
Arithmetic statements
See Arithmetic operators to learn how to use arithmetic operators.
Operator | Behavior |
---|---|
/ , % , * , - | Left and right operands are implicitly converted to numbers. |
+ | If either left or right operand is a text value, then both operands are implicitly converted to text. Otherwise, left and right operands are implicitly converted to numbers. |
++ , -- | The operand must be a number. No implicit casting occurs. If it is not a number, the result will be N . |
Assignment statements
See Assignment operators to learn how to use assignment operators.
Operator | Behavior |
---|---|
/= , %= , *= , -= | Left and right operands are implicitly converted to numbers. |
+= | If either left or right operand is text, then both operands are implicitly converted to text. Otherwise left and right operands are implicitly converted to numbers. |
Unary statements
See Unary operators to learn how to use unary operators.
Operator | Behavior |
---|---|
+ , - | The argument is implicitly converted to a number. |
Type conversion in code structures
- When calling functions, the parameters will be converted using implicit type conversion rules. If the function is overloaded, the overload will be determined before implicit conversion.
- For loop conditions must be boolean. There is no implicit type conversion.
- If conditions must be boolean. There is no implicit type conversion.