I was reading How can I get query string values in JavaScript? on Stackoverflow and this piece of code from the first reply made me wonder why ´vars.push()´ is used like this?
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
But instead of this:
var vars=[];
...
vars.push(hash[0]);
vars[hash[0]] = hash[1];
I rewrote the code like:
var vars={};
...
vars[hash[0]] = hash[1];
and it works. Now the questions are:
- why would someone use an array for that kind of reply?
- and why would someone use
ARR.push(KEY)
and then useARR[KEY]=VAL
format afterwards?