getURLParam in JavaScript: Retrieve URL Parameters

Have you ever found yourself needing to extract specific information from a URL using JavaScript? Then this article getURLParam in JavaScript in detail is for you.

Actually, whether you’re building a web application, enhancing user experience, or tracking analytics, retrieving URL parameters can be a common requirement.

In this article, we’ll explore how to efficiently extract URL parameters, manipulate URLs, and harness the power of JavaScript with the getURLParam function.

Let’s start!

What is geturlparam in javascript?

The function getURLParams() in Javascript is employed to retrieve the current URL parameters in JavaScript. It returns an object where each parameter is represented as an individual member of the object.

Syntax

getURLParams()

Parameter

This function does not take any arguments.

Return value

It provides an Object containing the path parameters as its return value.

How to Get URL Parameter in JavaScript?

To retrieve URL parameters in JavaScript, you can use the URLSearchParams API, which provides a convenient way to access and manipulate the parameters in a URL query string.

Here’s an example of how you can extract a specific parameter value by its name:

function getURLParam(paramName) {
  const params = new URLSearchParams(window.location.search);
  return params.get(paramName);
}

// Example usage:
const userId = getURLParam('id');
console.log(userId); // Output: The value of the 'id' parameter

In the example above, the getURLParam function takes a parameter name as an argument and uses the URLSearchParams API to retrieve the corresponding value from the URL’s query string.

Practical Examples

Now that we’ve covered the basics, let’s explore a few practical examples that combine the power of getURLParam with other JavaScript techniques:

Example 1: Extracting a Parameter Value

Suppose you have a URL like https://example.com/product?id=12345 and want to extract the id parameter value.

You can use the getURLParam function we discussed earlier:

const productId = getURLParam('id');
console.log(productId); // Output: 12345

Example 2: Removing a Parameter from URL

If you need to remove a specific parameter from a URL, you can modify the removeURLParams function we saw earlier:

function removeURLParam(paramName) {
  const url = new URL(window.location.href);
  const params = new URLSearchParams(url.search);
  params.delete(paramName);
  url.search = params.toString();
  return url.toString();
}

// Example usage:
const cleanURL = removeURLParam('id');
console.log(cleanURL); // Output: The base URL without the 'id' parameter

In this example, the removeURLParam function takes a parameter name as an argument, deletes that parameter from the URL, and returns the modified URL without the specified parameter.

How to Get Full URL in JavaScript?

If you need to obtain the complete URL of the current page, including the protocol, domain, path, query string, and fragment identifier, you can leverage the window.location.href property.

Here’s an example:

const currentURL = window.location.href;
console.log(currentURL); // Output: The full URL of the current page

The window.location.href property returns a string containing the complete URL, allowing you to access and use it in your JavaScript code.

How to Get URL String in JavaScript?

In some cases, you may only require the URL string without specific parameters or fragments. To extract the URL string in JavaScript, you can utilize the window.location.toString() method.

Here’s an example:

const urlString = window.location.toString();
console.log(urlString); // Output: The URL string of the current page

The window.location.toString() method returns the URL as a string, excluding any additional information like query parameters or fragment identifiers.

How to Remove All Parameters from URL in JavaScript?

At times, you may need to remove all the parameters from a URL, leaving only the base URL. JavaScript provides various approaches to achieve this.

One way is to use the URLSearchParams API along with the URL constructor to rebuild the URL without any query parameters.

Here’s an example:

function removeURLParams() {
  const url = new URL(window.location.href);
  url.search = '';
  return url.toString();
}

// Example usage:
const cleanURL = removeURLParams();
console.log(cleanURL); // Output: The base URL without any query parameters

The removeURLParams function creates a new URL object using the current page’s URL, sets the search property to an empty string, and returns the modified URL as a string.

Anyway here are some of the functions you might want to learn and can help you:

Conclusion

In conclusion, here are the key takeaways on this topic getURLParam in JavaScript:

  • The getURLParams() function in JavaScript retrieves the current URL parameters and returns them as an object.
  • To get URL parameters in JavaScript, you can use the URLSearchParams API and its get() method.
  • Practical examples include extracting a parameter value and removing a parameter from a URL.
  • To obtain the complete URL of the current page, use window.location.href.
  • To get the URL string without specific parameters or fragments, use window.location.toString().
  • To remove all parameters from a URL, you can use the URLSearchParams API along with the URL constructor.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Glay Eliver


Programmer & Technical Writer at PIES IT Solution

Glay Eliver is a programmer and writer at PIES IT Solution, author of over 600 tutorials at itsourcecode.com. Specializes in JavaScript tutorials, Microsoft Office how-tos (Excel, Word, PowerPoint), and Python error debugging covering ImportError, TypeError, AttributeError, ModuleNotFoundError, and JavaScript ReferenceError. Authored several of the site’s highest-traffic Excel and MS Office reference articles.

Expertise: JavaScript · MS Excel · MS Word · MS PowerPoint · Python · Python ImportError · Python TypeError · Python AttributeError · ModuleNotFoundError · JavaScript ReferenceError · Pygame
 · View all posts by Glay Eliver →

Leave a Comment