[Ethereum] How to get particular state of a contract through web3.js

contract-developmentsolidityweb3js

I deploy a simple token contract "MetaCoin through web3.js as follows:

var Web3 = require('web3');
var web3 = new Web3(Web3.givenProvider || 'http://localhost:8545');
const abi = [...];
var MetaCoin = new web3.eth.Contract(abi, "0x77e82a433403f2d3273f391414b1eafe7e792624");

I can call a function 'getBalance()' of the contract and get the result like this:

MetaCoin.methods.getBalance("0x77e82a433403f2d3273f391414b1eafe7e792624").call().then(function(result){ console.log(result); });

But how can I call a state like 'address public creator' defined in the contract?

Best Answer

When setting variables to public it automatically creates a getter method that can be used to query the variable.

contract Foo {
    uint public bar;
}

=>

myContractInstance.bar.call().then(function(result){ console.log(result); });