Skip to main content

$return

The $return operator returns a value from the current execution context, typically used for early exits from blocks.

Input

  • An optional single value to return (if omitted, returns null)

Output

  • The specified value, which causes an early exit from the containing block

Examples

Return Specific Value

{
  "$block": [
    {
      "$let": [
        "x",
        10
      ]
    },
    {
      "$ifelse": [
        {
          "$gt": [
            {
              "$var": ["x"]
            },
            5
          ]
        },
        {
          "$return": [
            "x is greater than 5"
          ]
        },
        "x is not greater than 5"
      ]
    },
    "This will not be executed if x > 5"
  ]
}
Result: "x is greater than 5" if x is greater than 5

Return Without Value

{
  "$block": [
    {
      "$return": []
    },
    "This will not be executed"
  ]
}
Result: null

Return Field Value

{
  "$block": [
    {
      "$ifelse": [
        {
          "$eq": [
            {
              "$input": "status"
            },
            "error"
          ]
        },
        {
          "$return": [
            {
              "$input": "error_message"
            }
          ]
        },
        {
          "$input": "result"
        }
      ]
    }
  ]
}
Result: The error_message field if status is “error”, otherwise the result field