Skip to main content

Variables

Defining variables is a fundamental part of any programming language. In this section, we will look at the different types of variables you can define in Solidity.

Defining variables

Variables are defined using the following syntax:

// <type> <name> = <value?>;
uint256 myVariable = 123;
uint256 myOtherVariable;

📘 Variable default values

If you do not assign a value to a variable, it will be assigned a default value. The default value depends on the type of the variable. You can generally infer the default value from the type name. For example, uint256 will default to 0, and bool will default to false. Keep this in mind when writing your smart contracts, as you might not expect an integer to have a value of 0 when you first define it.

Integers & boolean

Integer types are used to represent whole numbers. They can be either signed (positive or negative) or unsigned (positive only).

Integer TypesDescription
boolBoolean (true/false)
int8Signed 8-bit integer
int16Signed 16-bit integer
int32Signed 32-bit integer
int64Signed 64-bit integer
int128Signed 128-bit integer
int256Signed 256-bit integer
uint8Unsigned 8-bit integer
uint16Unsigned 16-bit integer
uint32Unsigned 32-bit integer
uint64Unsigned 64-bit integer
uint128Unsigned 128-bit integer
uint256Unsigned 256-bit integer

Address

Address types are used to represent Ethereum addresses. They can be either address or address payable.

Address TypesDescription
addressEthereum address
address payableEthereum address that can send and receive Ether

Fixed-size byte arrays

Fixed-size byte arrays are used to represent a sequence of bytes. They can be either bytes or bytes32.

Fixed-size byte arraysDescription
bytesDynamic sequence of bytes
bytes1 to bytes32Fixed-size sequence of bytes
stringDynamic sequence of UTF-8 bytes

📘 Enum underlying values

Each value in an enum has an underlying value. By default, the first value has an underlying value of 0, and each subsequent value has an underlying value that is one greater than the previous value.

Structs

Struct types are used to represent a custom data container. They can be defined using the following syntax:

struct MyStruct {
uint256 myUint;
bool myBool;
}

Arrays

Arrays are used to represent a collection of values. They can be either fixed-size or dynamic.

ArraysDescription
uint256[]Dynamic array of unsigned 256-bit integers
uint256[5]Fixed-size array of unsigned 256-bit integers with a length of 5

Though the list above only shows arrays of integers, you can define arrays of any type.

bool[] myBoolArray;
address payable[] myAddressArray;

You may also define multi-dimensional arrays.

uint256[][] myMultiDimensionalArray;

Accessing/assigning arrays is done using the following syntax:

myArray[index] = 123;
myOtherArray[indexA][indexB] = 456;

Mappings

Mappings are used to represent a key-value store. They can be defined using the following syntax:

// mapping(<key type> => <value type>) <name>;
mapping(address => uint256) myMapping;

You can also define multi-dimensional mappings.

mapping(address => mapping(address => uint256)) myMultiDimensionalMapping;

Accessing/assigning mappings is done in the same way as accessing arrays.

myMapping[myAddress] = 123;
myOtherMapping[myAddress][myOtherAddress] = 456;

Enums

Enum types are used to represent a fixed set of values. They can be defined using the following syntax:

enum MyEnum {
Value1,
Value2,
Value3
}