Check out v4 of the Fauna CLI

v4 of the Fauna CLI is now in beta.

The new version introduces enhancements to the developer experience, including an improved authentication workflow. To get started, check out the CLI v4 quick start.

Variables and identifiers

This section describes the syntax for variable declaration and the rules for associating named identifiers with variables.

Variables

Variables are used to store values that are one of the FQL Types.

You assign a value to the variable using the let statement:

let foo = 5

By default, variables are immutable so you can’t assign a new value to the variable. Assigning a new value to the variable x gives an error:

let x = 5
x = 6
invalid_query: The query failed 1 validation check

error: Expected end-of-input
at *query*:2:3
  |
2 | x = 6
  |   ^
  |

But you can redeclare a variable:

let a = 2
let a = 4
a
4

You can also use a previous definition of the variable to redeclare the variable:

let x = 1
let x = x + 1
x
2

Here is a more complex redefinition example using an anonymous function, which shows the effect of the order of declaration:

let x = "foo"
let fn = () => x
let x = "bar"
let y = fn()
[y, x]
[
  "foo",
  "bar"
]

Variables are scoped to the block in which they’re declared as shown in this example:

let x = "foo"
let y = {
  let x = "bar"
  x  // a block must end with an expression
}
[x, y]
[
  "foo",
  "bar"
]

Identifiers

An identifier associates a name with a value. The name can be used to refer to a variable, property, field, or resource.

  • An identifier must start with a character from the set [a-zA-Z_] and be followed by zero or more characters from the set [a-zA-Z0-9_].

    An identifier defined with the let statement can’t be a single underscore (_) character. The single underscore is allowed as an anonymous function parameter and can be repeated in an anonymous function parameter list.

  • Identifiers are case-sensitive.

  • An identifier can’t be a reserved word except as described in Naming and aliasing.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!