I've been reading and trying but to no avail. I'm trying to compare the current wallet address in MetaMask with the wallet address which owns a certain token via a smart contract method "ownerOf" which returns the address of the token owner.

I tried using "valueOf()", "JSON.stringify()", toString() and also "localcompare" and none worked. Localcompare always return a value of more than 0 even the two addresses match.

Below is my code.

async function doesTokenIdExist(tokenId, contract, walletAddress) {

  await contract.methods.ownerOf(tokenId).call(function(err,res){

            console.log("Output of localeCompare: " + res.localeCompare(walletAddress));

            if (tokenAddress.localeCompare(walletAddress) == 0){
                console.log("Wallet owner owns tokenId");

        } else {


1 Answer 1


Maybe you should use toLowerCase() on both addresses, and better to console.log them first and see if one of them has "0x" or if they already equal

  • Gosh! You're a genius. I actually been outputting every single output I could and didn't realize the case. Yeah, some addresses have all small caps while some mix of big and small caps. Never thought blockchain coding is soo tricky.
    – warnerque
    Commented May 16, 2019 at 13:56
  • you are welcome, it happen to me alot ;-)
    – Majd TL
    Commented May 16, 2019 at 14:42

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.