JavaScript: Get an object containing the parameters of the current URL
JavaScript fundamental (ES6 Syntax): Exercise-51 with Solution
Get Current URL Parameters as Object
Write a JavaScript program to get an object containing the current URL parameters.
- Use String.prototype.match() with an appropriate regular expression to get all key-value pairs.
- Use Array.prototype.reduce() to map and combine them into a single object.
- Pass location.search as the argument to apply to the current url.
Sample Solution:
JavaScript Code:
Output:
{"name":"Adam","surname":"Smith"} {} {}
Flowchart:

Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript program that parses the current page URL and returns an object containing its query parameters.
- Write a JavaScript function that converts a URL's search string into a key-value object using URLSearchParams.
- Write a JavaScript program that extracts URL parameters and handles cases with duplicate keys by storing values in an array.
Go to:
PREV : Integer to Suffix String with am/pm.
NEXT : Group Array Elements by Function.
Improve this sample solution and post your code through Disqus
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.