Enums

Learn to work with enums in Solidity.

We'll cover the following...

Enums, or enumerations, offer a convenient way to establish custom types comprising a set of named constants. Enums come in handy when we wish to define a variable that should only assume one among a small array of potential values. This clarity enhances the safety of our contracts because enums restrict variables to a known set of values and thus reduce the likelihood of errors.

The typical syntax for defining an enum in Solidity looks like this:

enum <EnumName> {
Choice1,
Choice2,
Choice3,
// ...
}
The syntax for declaring an enum in Solidity
  • enum: This keyword tells Solidity that we’re crafting a new enumeration.

  • ...