Recently, I have been revisiting Solidity, consolidating the finer details, and writing "WTF Solidity" tutorials for newbies.
Twitter: @0xAA_Science | @WTFAcademy_
Community: Discord|Wechat|Website wtf.academy
Codes and tutorials are open source on GitHub: github.com/AmazingAng/WTF-Solidity
Initial values of variables
In Solidity, variables declared but not assigned have their initial/default values. In this tutorial, we will introduce the initial values of common variable types.
Initial values of value types
boolean
:false
string
:""
int
:0
uint
:0
enum
: first element in enumerationaddress
:0x0000000000000000000000000000000000000000
(oraddress(0)
)function
internal
: blank functionexternal
: blank function
You can use the getter
function of public
variables to confirm the above initial values:
Initial values of reference types
-
mapping
: amapping
which all members set to their default values -
struct
: astruct
which all members set to their default values -
array
- dynamic array:
[]
- static array(fixed-length): a static array where all members are set to their default values.
- dynamic array:
You can use the getter
function of public
variables to confirm initial values:
delete
operator
delete a
will change the value of variable a
to its initial value.
Verify on Remix
-
Deploy
InitialValue.sol
and check the initial values of the different types. -
After using the
delete
operator, the values of the variables are reset to their initial values.
Summary
In this section, we introduced the initial values of variables in Solidity. When a variable is declared but not assigned, its value defaults to the initial value, which is equivalent to 0
represented in its type. The delete
operator can reset the value of the variable to the initial value.