You are reading a single comment by @Alex and its replies. Click here to read the full conversation.
  • Hi all,

    I have encountered a strange problem regarding indices. I have a small snippet of example code that shows this problem:

    var a = {};
    var c = {};
    
    var b = "0";
    
    a[b] = "hello world";
    c["0"] = "hello moon";
    
    console.log(a[b],a[0],a["0"]);
    // output: hello world undefined undefined
    
    console.log(c[b],c[0],c["0"]);
    // output: undefined hello moon hello moon
    
    function foo(id) {
     console.log(id,a[id], c[id]);
    }
    
    foo(b);
    // output: 0 hello world undefined
    
    foo("0");
    // output: 0 hello world undefined
    

    I would expect that a[b] and a["0"] would give the same result?

    Thanks for all the help so far!

About

Avatar for Alex @Alex started