Skip to main content

$let

The $let operator declares a variable in the current scope.

Input

  • An array containing exactly two values: the variable name (string) and the value to assign to the variable

Output

  • The assigned value

Examples

Basic Variable Assignment

{
  "$let": [
    "x",
    42
  ]
}
Result: 42

Assign Field Value to Variable

{
  "$let": [
    "user_name",
    {
      "$input": "name"
    }
  ]
}
Result: The value of the name field, which is also stored in the user_name variable

Use in a Block

{
  "$block": [
    {
      "$let": [
        "total",
        {
          "$add": [
            {
              "$input": "price"
            },
            {
              "$multiply": [
                {
                  "$input": "price"
                },
                0.1
              ]
            }
          ]
        }
      ]
    },
    {
      "$var": ["total"]
    }
  ]
}
Result: The calculated total value including tax