I thought I knew JavaScript pretty decently, until I encountered other developer's code which knocked me down:
var data = [];
As you can see by its name, it's supposed to be used as an associative array (i.e. Object
), but it is an Array
. And then he assigns values to keys of that array:
data['somekey'] = 'somevalue';
I thought that wasn't possible in JavaScript, I thought it would throw an exception, but it works. Why does it work? Why do we need Objects then, if we can use Arrays instead? Is it considered a bad practice, and should I shame that developer?