Solidity Default Values
A default value is the value a state variable holds before any code sets it. Every type in Solidity has its own starting value.
An unset number reads as 0
This contract stores a score and never sets it. Reading the score still gives a clear answer.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract UnsetScore {
uint256 public score;
}An unset flag reads as false
A bool flag also starts with a set answer. An unset flag reads as false.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract UnsetFlag {
bool public ready;
}An unset address and text read as empty
An address starts as the empty address. A string starts as empty text.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract UnsetOwner {
address public owner;
string public label;
}Example
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract NewPlayer {
uint256 public points;
bool public active;
address public wallet;
string public nickname;
}Key points
- Contract: a program stored on a blockchain, which is a shared record kept by many computers.
- Default value: the starting value Solidity gives a variable that was never set.
- Number default: an unset uint256 reads as 0.
- Flag default: an unset bool reads as false.
- Address default: an unset address reads as address(0), the empty address.
- Text default: an unset string reads as empty text with no characters.
In this example, the contract declares four state variables and sets none of them. The number points reads as 0. The flag active reads as false. The address wallet reads as address(0). The string nickname reads as empty text.
Note
Solidity never leaves a state variable without a value to read.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Remix IDE
Exercise
A contract declares uint256 public score and never sets it. What does score() return?