Search code examples
javascriptcomparison

comparing hex string all return false


I am comparing the hex string below value:

'0xC6b250Dd4dD3372C6602200C78D900818627eC9e'
'0xC6b250Dd4dD3372C6602200C78D900818627eC9e'
'0x570324fd11272F2F980AfeBf09D680aC9F136B1k'

using:

if(value === '0x570324fd11272f2f980afebf09d680ac9f136b1k');

Why all return false? because the last one is same?


Solution

  • JavaScript string comparison is case sensitive. You can either normalize value by converting to lower case characters before comparing with value.toLowerCase().

    Example:

    const values = ['0xC6b250Dd4dD3372C6602200C78D900818627eC9e', '0xC6b250Dd4dD3372C6602200C78D900818627eC9e', '0x570324fd11272F2F980AfeBf09D680aC9F136B1k'];
    
    console.log(values.map(value => value.toLowerCase() === '0x570324fd11272f2f980afebf09d680ac9f136b1k'));