Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
How can I convert a JavaScript object into a query string for a URL?
Asked on Jan 23, 2026
Answer
To convert a JavaScript object into a query string for a URL, you can use the `URLSearchParams` API, which provides a convenient way to handle URL query parameters.
<!-- BEGIN COPY / PASTE -->
const params = { name: "John Doe", age: 30, city: "New York" };
const queryString = new URLSearchParams(params).toString();
console.log(queryString); // Output: "name=John+Doe&age=30&city=New+York"
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- The `URLSearchParams` constructor takes an object and converts it into a query string.
- The `toString()` method is used to get the query string representation.
- The output query string is URL-encoded, which means spaces are converted to "+" and special characters are encoded.
Recommended Links:
