0

I am new to JavaScript. In the following code snippet (from here), what does the [], hash syntax mean? I searched a bit but no luck.

function getUrlVars()
{
    var vars = [], hash;// <============= HERE
    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;
}
Community
  • 1
  • 1
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • `var vars = []` is defining an empty array called `vars`, and `hash` is a local variable with a value of `undefined`. It's the same as `var vars = [];` and `var hash;`. – Josh Crozier Dec 04 '15 at 04:09
  • 1
    as far as the rest of the code is concerned, it's a bit of a wreck: two `var` statements with one having multiple declarations, incorrectly parsing the URL for the query string (could just be `location.search`), [incorrectly parsing the query string](http://zzzzbov.com/blag/querystring-hell), trying to use an array as a hash map instead of an object. Get rid of this code and use a lib. – zzzzBov Dec 04 '15 at 04:15
  • @zzzzBov I am trying do some common tasks on a page, such as get query string parameters. Any lib to recommend? I am not quite familiar with the JavaScript landscope. – smwikipedia Dec 04 '15 at 05:04
  • In NPM land (node, browserify, etc) the [querystring](https://www.npmjs.com/package/querystring) library is solid enough for most usage, but there are some edge cases that it doesn't handle, so [I wrote my own querysting parsing lib](https://www.npmjs.com/package/querystringjs) which uses UMD. – zzzzBov Dec 04 '15 at 15:21

3 Answers3

4

This is just a normal declaration of multiple variables

var vars = [], hash;

is same as

var vars = [];
var hash;

MDN

Syntax:

var varname1 [= value1 [, varname2 [, varname3 ... [, varnameN]]]];
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • It seems `vars` are intended to use as `dictionary`. But based on my limited knowledge about JS object, it's better not to use a true array. right? – smwikipedia Dec 04 '15 at 04:09
  • @smwikipedia `vars` is an array, in JS everything(well, _not everything_) is an object, so you can also assign properties to the array. – Tushar Dec 04 '15 at 04:11
1

You're looking at [], hash when you should be looking at the entire line, which is:

var vars = [], hash;

In its entirety, you have a variable declaration, which takes a comma separated list of variable names. Those names being vars and hash, which would be better formatted as:

var vars,
    hash;

And within that declaration you have a variable assignment to the vars variable.

[] is an array literal in JavaScript. So in the end you have declared vars, initialized it with a value of a new empty array, and declared hash without initializing it (value will be undefined).

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
0

That code just combined these two concepts to make the code impossible to understand for newbies:

1. There are 2 ways to declare an Array:

// 1 (old school)
var myArray = new Array();

// 2 (the easiest and most common)
var myArray = [];

2. You can declare multiple variables (with or without setting their values) in one swoop using a comma:

// Declaring 3 variables
var a, b, c;
// or
var a=1, b=2, c=3;
bob
  • 7,539
  • 2
  • 46
  • 42