When I run this code in web3

web3.eth.getAccounts().then(e => console.log(e));

The console returns this:

 [ '0x772f437b0f15e1F205C8BD923b5C8357e9c0c429',
    '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


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.

7 Answers 7


Since web3.eth.getAccounts() returns a list of addresses you can access the first accounts's address easily just by e[0] in your example.

So just try this: web3.eth.getAccounts().then(e => let firstAcc=e[0]; console.log(firstAcc));

Have a look at the official documentation of the getAccounts() web3js function here.

  • Hi Istvan, I got the desired output on the terminal. However, it seems that the variable firstAcc resets its own value once the program exits the getAccounts() function (See the EDIT for reference). The web3.js documentation doesn't seem to explain why that is the case. Is there a way to overcome this problem? Sorry for asking such basic question, I really appreciate the help.
    – potatoguy
    Commented Nov 29, 2017 at 11:24
  • 1
    This is not a web3.js related issue rather just a regular javascript phenomena. The function which body is after the .then is called the callback function. In your edited question first the second console.log is executed and at the time of its execution indeed firstAccount is undefined, this is no surprise! However, when the callback function is returned and executed, obviously we will have the correct value in the firstAccount variable. I guess you should just get to know better callback functions. You could start at thousand places, there are plenty of good resources for Javascript Commented Nov 29, 2017 at 12:08
  • A more interesting question is, how do you get the 11th account address if you have 11+ accounts? Commented Aug 1, 2018 at 20:01
  • web3.eth.getAccounts((err, res) => {accounts = res[0]}) Commented Aug 31, 2018 at 20:30

you can do:

var address = web3.eth.accounts[0];

to get the first address in in curret account

  • 1
    I believe "web3.eth.accounts" has been deprecated, no? In any case, it did not return the first account. When I print it on the console, it says "undefined." Thanks for answering though.
    – potatoguy
    Commented Nov 29, 2017 at 10:06
  • 1
    web3.eth.accounts[0]; definitively return the first account address.
    – btc4cash
    Commented Nov 29, 2017 at 10:08
  • @btc4cash No it doesn't. Commented Jul 2, 2018 at 7:18
  • Prove it please!
    – btc4cash
    Commented Jul 2, 2018 at 13:35
  • @btc4cash, John is saying right so don't argu if you are not sure about your answer. Commented Aug 31, 2018 at 20:27

It is very important to take note of the version of web3 you are using because there are changes to be reckoned with!

CASE 1: web3.js 0.2x.x (Stable)

For web3 0.2x.x https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethaccounts

var address = web3.eth.accounts[0]; 

and the async (asynchronous) version can be used,

web3.eth.getAccounts(callback(error, result){ ... });

They are both read-only and return a list (i.e. Array) of accounts the node controls.

Here is the Problem:

web3.eth.getAccounts().then(e => console.log(e));

will fail because getAccounts implements a callback not a promise.

CASE 2: web3.js 1.0 ( A work in Progress)

getAccounts returns Promise which returns Array - An array of addresses controlled by node. https://web3js.readthedocs.io/en/1.0/web3-eth.html#getaccounts Supports both Promise and Callback

web3.eth.getAccounts(callback(error, result){ ... }); // callback
web3.eth.getAccounts().then(e => console.log(e)); // Promise

In Truffle Console

web3.eth.getAccounts().then( function(s){FirstA=s[0]})

then You can use FirstA variable



You can use


style in geth command window to get the account.

You should use


style in Node JS to get the account.

  • 1
    first one works in web3 0.20.x, second one is used in web3 1.0.x
    – sensei
    Commented Dec 10, 2018 at 11:17

For those who are still looking for an easy answer:

let accounts = await web3.eth.getAccounts()

will assign whole array to accounts. Then you can access as accounts[0] to the first one


Note: I'm using truffle console.

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.