This is a part of ERC20.sol of OpenZeppelin contract.

mapping(address => uint256) private _balances;

mapping(address => mapping(address => uint256)) private _allowances;

I am learning programming from scratch so it might be a beginner question, but I really do not understand what roles this command plays. Would there be other commands as replacement than mapping?

1 Answer 1


Mapping is the core concept of solidity, it is almost the same as dictionary in python or an object in javascript.

The idea is to keep track of values using the keys key : value. So when you need that value, just give it the key and it will return the value.

mapping(address => uint256) private _balances - address is the key and uint256 is the value. So each address is associated with some number, in this case, balance. If you want to retrieve someone's balance, give it the address and it will return the balance:

To retrieve that uint256 value from a given mapping, you just call _balances[address] and voila. If you want to populate that address with a new balance, do the same thing _balances[address] = 1000.

mapping(address => mapping(address => uint256)) private _allowances; This is nested mapping, more advanced technique to keep track of things. Once you understand how mapping works with examples, you will quickly grasp the concept of nested mappings as well.

And to respond to your question, no there is no other alternative to mapping. It's a unique way to keep track of data. Some might say there is an array as well, and it is somewhat true. In an array, you have the index as the key, and the value itself, but the index can only be a numerical value and as you saw in mapping, key can be anything.

  • Thank you so much it helped me a lot. I understood the necessity of mapping as more like a flexible chain (compared to using array instead) and I hope I got it in the right direction. Thanks!
    – jchoo0x
    Commented Apr 18, 2022 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.