Take the 2-minute tour ×

I want to stringify a querystring.

For example,

{ hello: '123', goodbye: "789" }

The library would give me...

share|improve this question
It should be doable with loop and join. I don't think I have come across built-in JS function to do this, though. –  nhahtdh Mar 1 '13 at 18:30
add comment

4 Answers

up vote 5 down vote accepted

There is an excellent URL library, URL.js which works pretty much as you describe for queries.

For your example, the code would be:

URI().addSearch({ hello: '123', goodbye: "789" }).toString()

This produces the result with a pre-pended ?, but it comes in extremely handy for constructing & manipulating real URLs.

share|improve this answer
+1 for answering the actual question. –  Peter Bratton Mar 1 '13 at 18:33
+1 good library –  karaxuna Mar 1 '13 at 18:33
add comment

If case if you use jQuery in your project you don't need a lib for this:

$.param({ hello: '123', goodbye: "789" })

But since there is no jQuery tag, take a look at Barney answer, this is probably what you need.

share|improve this answer
add comment

Here is a code sample for simple cases:

var params={ hello: '123', goodbye: "789" },
for (var key in params) {
    if (params.hasOwnProperty(key)) {
var qs=pairs.join("&");

For more complex scenarios you might want to use a library like URLjs as suggested in other replies.

share|improve this answer
add comment

I really like this one: BBQ - it's been around for a while and works well.

share|improve this answer
add comment

Your Answer


