I am simulating a 4 dimensional array and am having a small issue which I can't seem to figure out. As an example: var test = []; test[0000] = 1; test[0100] = 2; a = 0+''; b = 0+''; c = 0+''; d = 0+''; e = a+b+c+d; alert(e); alert(test[e]); alert(test[0000]); Code (markup): If you run this javascript I get 3 alerts as follows: 1) 0000 2) Undefined 3) 1 Can anyone explain why that second alert is showing undefined when in theory it's the same as the 3rd one.
I guess javascript is using associative arrays with strings, but treating e as a number, so test[e] is undefined. You can get it worked using strings explicitly: var test = []; test['0000'] = 1; test['0100'] = 2; a = '0'+''; b = '0'+''; c = '0'+''; d = '0'+''; e = a+b+c+d; alert(e); alert(test[e]); alert(test['0000']); Code (markup):