Version: 0.41.0

Constants & Variables

The next building block after types are constants and variables.


Constants are immutable by design, which means their values cannot be reassigned. Put in another way, they can be assigned once, at their declaration. When defining a constant you need to provide a name, type and a value:

Constants in JsLIGO are enforced:

let x = (a : int) : int => {
const age : int = 25;
age = 3; // Yields an error

Unlike the other syntaxes, JsLIGO doesn't allow variable names to be reused in the same block scope:

let x = (a : int) : int => {
const age : int = 25;
const age : int = 3; // Yields an error

However, the following does work:

let x = (a: int): int => {
const age: int = 25;
const age : int = 3; // does not give an error
return age;

You can evaluate the constant definition above using the following CLI command:

ligo run evaluate-expr gitlab-pages/docs/language-basics/src/variables-and-constants/const.jsligo --entry-point age
# Outputs: 25


Variables, unlike constants, are mutable.

⚠️ Please be wary that mutation only works within the function scope itself, values outside of the function scope will not be affected. In other words, when a function is called, its arguments are copied, as well as the environment. Any side-effect to that environment is therefore lost when the function returns.

let add = (a: int, b: int): int => {
let c = a;
c = c + b;
return c

You can run the add function defined above using the LIGO compiler like this:

ligo run evaluate-call gitlab-pages/docs/language-basics/src/variables-and-constants/add.jsligo '(1,1)' --entry-point add
# Outputs: 2

Escaped Identifiers#

Both variables and constants are, at the level of the lexicon, identifiers. Each flavour of LIGO has its own set of keywords, PascaLIGO with the most. Sometimes we need an identifier that is the same as a keyword, or, perhaps, we do not want to shadow a predefined identifier, like amount. In those cases, you could suffix your identifier with an underscore, like amount_. (Beware that if you prefix with an underscore, like _amount, the compiler will not complain about the value being not used.) But this is not a good practice because we do not pronounce aloud the underscores, and there is the issue of one or two underscores. To solve all those problems, in LIGO, you can prefix you identifier with @, like @amount.