This may be javascript question, but I am totally out of ideas.

I have a function in solidity code returning a variable:

  function getIdentifier() public view returns (string) {
    return identifier;

I want to simply call this function with web3.js and store result as a variable. But all I can do is console.log the result (I know, I know =/).

Following code console.logs the variable:


Works fine, but I simply want to store the return of getIdentifier() in a variable x.

None of the following naive code work:

x = contractInstance.methods.getIdentifier();
x = contractInstance.methods.getIdentifier().call();

I am sorry for a dumb question, but I have tried tons of things and it still does not work.

EDIT: Thanks smarx your code works, but I still would like to keep the variable for later use. Strangely something like this does not work:

var y = "";
.then(function (x) {
  y = x;
console.log(y) // still empty!!! :(

5 Answers 5


This is just a JavaScript question. :-)

.then(function (x) {
  // Use x in here.

// This code runs BEFORE the callback, and x won't be defined out here.
  • Thank you for the reply. It works if I use ....getIdentifier().call().then( ..., but this does not quite solve my problem as I would like to use the variable later on in the code. Could you check my updated answer.
    – Winten
    Jan 27, 2018 at 13:48
  • The comment in my code already anticipated that confusion. :-) Your console.log happens before you've assigned anything to y. Put the code that uses x inside the then function.
    – user19510
    Jan 27, 2018 at 17:39

Let's say the abi of my contract is 'abi' and the variable I am using to interact with my contract is 'agreem'. The address of deployed contract is contract_address.

let agreem =  new web3.eth.Contract(abi, contract_address);

Now, I will say my function's name is retrieve and it returns two values. My function takes two parameters as well. To call my function-

const bal=await agreem.methods.retrieve(uname,ps).call({from:accounts[0]});

The variable bal will store both the return values returned by my function.To access them you can use bal[0] and bal[1] .

Hope it helps.


You need your ABI and your address saved as a string

You create the contract instance like this:

var contract = web3.eth.contract(JSON.parse(abi)).at(address);

You can save the contract call like this:


Do you use truffle to develop? Truffle calls are different from web3js calls.

EDIT: This solution works with plain web3.js not truffle


Here is an example:

contract("MyContractTest", () => {
    let myContract;

    before(async () => {
        myContract = await artifacts.require("MyContract.sol").new(construction arguments);

    describe(`test:`, async () => {
        it(`getIdentifier:`, async () => {
            let identifier = await myContract.getIdentifier();

If the function is not pure/view/constant, then it's a little more complicated (you need to add an event in the Solidity contract, and extract it from the returned value in the Javascript test).

var x = await contractInstance.methods.getIdentifier().call();

Though this will work only in async function. In synchronous code yuu will need to use callback:

contractInstance.methods.getIdentifier().call().then (x => {/* use x here */});

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.