How to get an object containing parameters of current URL in JavaScript ? Last Updated : 15 Feb, 2023 Comments Improve Suggest changes Like Article Like Report The purpose of this article is to get an object which contains the parameter of the current URL. Example: Input: www.geeksforgeeks.org/search?name=john&age=27 Output: { name: "john", age: 27 } Input: geeksforgeeks.org Output: {} To achieve this, we follow the following steps. Create an empty object.Using the String.match() method extract all the query params which are separated by ? and &, to achieve this we use the regex /([^?=&]+)(=([^&]*))/gThe String.match() method returns an array containing all the queries.Using the for...each loop iterates the array and at every iteration split the value at = sign by using the String.split() method. This method returns an array of 2 strings the 0'th string is the left part of the = sign and the 1st string is the right part of the = sign.Assign the first string as the key and the second string as the value of that key in the newly created object.Finally, return the newly created object. Example: JavaScript function getAllParams(url) { // Create an empty object let obj = {}; // Extract the query params let paramsArray = url.match(/([^?=&]+)(=([^&]*))/g) // Check if there is one or more params if (paramsArray) { // Iterate the params array paramsArray.forEach((query) => { // Split the array let strings = query.split("=") // Assign the values to the object obj[strings[0]] = strings[1] }) } // Return the object return obj; } console.log(getAllParams( "www.geeksforgeeks.org/search?name=john&age=27")) console.log(getAllParams("geeksforgeeks.org")) Output: { age: "27", name: "john" } {} Comment More infoAdvertise with us Next Article How to get an object containing parameters of current URL in JavaScript ? devi_johns Follow Improve Article Tags : JavaScript Web Technologies javascript-object JavaScript-Methods JavaScript-Questions +1 More Similar Reads Convert URL parameters to a JavaScript Object Given an URL with parameters, The task is to get those parameters and convert them to a JavaScript Object using javascript. we're going to discuss a few techniques. Below is the following method to convert URL parameters to a JavaScript Object: Using replace() MethodUsing split() MethodUsing for ... 2 min read How to Pass Object as Parameter in JavaScript ? We'll see how to Pass an object as a Parameter in JavaScript. We can pass objects as parameters to functions just like we do with any other data type. passing objects as parameters to functions allows us to manipulate their properties within the function. These are the following approaches: Table of 3 min read How to get dynamic access to an object property in JavaScript ? In JavaScript, an object is a collection of properties, where each property consists of a key-value pair. Objects are a fundamental data type in JavaScript. You can create an object in JavaScript in the following ways: By using the object literal notation, which is a comma-separated list of key-valu 7 min read How to Get Domain Name From URL in JavaScript? In JavaScript, the URL object allows you to easily parse URLs and access their components. This is useful when you need to extract specific parts of a URL, such as the domain name. The hostname property of the URL object provides the domain name of the URL.PrerequisiteJavascriptHTMLBelow are the fol 2 min read How to read properties of an Object in JavaScript ? Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl 2 min read How to serialize an object into a list of URL query parameters using JavaScript ? Given a JavaScript Object and the task is to serialize it into a URL query parameters using JavaScript. Approach 1: Declare an object and store it in the variable.Then use JSON.stringify() method to convert a JavaScript object into strings and display the content.Next, take an empty string and appen 2 min read How To Get URL And URL Parts In JavaScript? In web development, working with URLs is a common task. Whether we need to extract parts of a URL or manipulate the URL for navigation, JavaScript provides multiple approaches to access and modify URL parts. we will explore different approaches to retrieve the full URL and its various components.The 3 min read JavaScript - How to Return Current URL for Share Button? In this article, we will learn how to create share buttons on websites, allowing users to share a post or the URL of the site on various social media platforms. We will break this down into simple steps for better understanding.Step 1: Create the HTML StructureFirst, we need to create an HTML file t 4 min read How to get the first key name of a JavaScript object ? In JavaScript, accessing the first key name of an object involves identifying the initial property defined within that object. This concept is useful when you need to interact with or manipulate object properties in a specific order, particularly when the sequence of properties is relevant.Here we h 2 min read How to get URL Parameters using JavaScript ? To get URL parameters using JavaScript means extracting the query string values from a URL. URL parameters, found after the ? in a URL, pass data like search terms or user information. JavaScript can parse these parameters, allowing you to programmatically access or manipulate their values.For getti 3 min read Like