Solidity theorytheory 0/50 · 0%
Types · easy

9. Mappings

Key/value state, defaults and why you cannot iterate.

A mapping is a hash table from key to value. Every possible key exists and returns the zero value until written, so there is no length and no way to iterate.

mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;

If you need to enumerate holders, keep a parallel array or emit events and index them off chain.

Check your understanding

  1. 1. What does an unwritten mapping key return?

  2. 2. Can you iterate a mapping in Solidity?

  3. 3. How do you track holders for enumeration?