When I run this code in web3
web3.eth.getAccounts().then(e => console.log(e));
The console returns this:
[ '0x772f437b0f15e1F205C8BD923b5C8357e9c0c429',
'0x5738d9100A6CB64FB78730746ab61472c7808fD9',
'0x7086D052EAaD359c7aCD9B993c6169aE0dEC0725',
'0x9fA18751b024FDC55cC85A484fF4261351Dd4666',
'0x95a29de4A8cc4697E627922d270Aa74C2bd7494a',
'0x332c22e9c7F02e092b18C6cc4D9Bfd46d36Dd7D9',
'0xaED2F48c0d06a13D1FE2853E7Ee3B7d0aA328d3B',
'0x88A0941f0dcb501C20b7BaE5a41608EB981f1209',
'0xad5d1cB78e80518b596A814340826F36B89660Fc',
'0x67E23e936C9d22eDCf9ebC9A989911b313c24BC1' ]
But what if I only want the first account in the array and assign it to a variable?
In this case, it's 0x772f437b0f15e1F205C8BD923b5C8357e9c0c429
EDIT:
I have got the correct output after following the instruction of István András Seres, below is my code:
var firstAccount;
web3.eth.getAccounts().then(e => {
firstAccount = e[0];
console.log("A: " + firstAccount);
})
console.log("B: " + firstAccount);
Strangely, the output is:
A: [correct testrpc account]
B: undefined
It seems that a variable is not allowed to carry the same value once it exits getAccounts() function.