URL
parameters
web development
query strings
coding tutorial

Retrieving parameters from a URL

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Retrieving parameters from a URL usually means reading the query string, the part after ?, such as ?page=2&sort=desc. Modern web APIs make this straightforward, but it still helps to understand the difference between path segments, query parameters, and fragments so you read the right part of the URL.

What Counts as a URL Parameter

Consider this URL:

text
https://example.com/products?category=books&page=2#reviews

Its pieces are:

  • path: /products
  • query string: category=books&page=2
  • fragment: reviews

When people say "URL parameters," they usually mean the query string key-value pairs:

  • 'category=books'
  • 'page=2'

The fragment after # is not sent to the server in a normal HTTP request, so do not confuse it with query data.

Modern JavaScript: URLSearchParams

In browser JavaScript, the cleanest solution is URLSearchParams.

javascript
1const params = new URLSearchParams(window.location.search);
2
3const category = params.get("category");
4const page = params.get("page");
5
6console.log(category);
7console.log(page);

If the page URL is:

text
https://example.com/products?category=books&page=2

then the output is:

text
books
2

This is the standard answer for modern front-end code.

Getting Multiple Values

Some parameters can appear more than once:

text
https://example.com/search?tag=python&tag=pandas&tag=data

In that case, get() returns only the first value. Use getAll() if you want every one.

javascript
const params = new URLSearchParams("?tag=python&tag=pandas&tag=data");
console.log(params.get("tag"));
console.log(params.getAll("tag"));

Output:

text
python
['python', 'pandas', 'data']

This matters for filter UIs and API designs that allow repeated keys.

Server-Side Example in Python

On the server side, you usually parse the query string from the request URL rather than from window.location.

Here is a standard-library Python example:

python
1from urllib.parse import urlparse, parse_qs
2
3url = "https://example.com/products?category=books&page=2&page=3"
4
5parsed = urlparse(url)
6params = parse_qs(parsed.query)
7
8print(params["category"])
9print(params["page"])

Output:

text
['books']
['2', '3']

Notice that parse_qs returns lists because repeated parameters are allowed.

Handling Missing Parameters

Parameters are often optional, so code should handle the missing case explicitly.

javascript
1const params = new URLSearchParams(window.location.search);
2const page = params.get("page") ?? "1";
3
4console.log(page);

Or in Python:

python
page = params.get("page", ["1"])[0]
print(page)

Default handling like this keeps your application from crashing or silently producing null-like values in the wrong place.

Query Parameters Are Strings

A subtle but important detail is that query parameters arrive as strings. If you need a number or boolean, parse it yourself.

javascript
1const params = new URLSearchParams("?page=2&debug=true");
2const page = Number(params.get("page"));
3const debug = params.get("debug") === "true";
4
5console.log(page + 1);
6console.log(debug);

Do not assume the runtime converts types automatically just because the text looks numeric.

Encoding and Safety

URL parameters are encoded. For example, spaces are often represented as %20 or +. Good parsing APIs decode this for you automatically, which is another reason to avoid manual string splitting when a real URL parser is available.

Also remember that query parameters are untrusted input. Validate them before using them in database queries, file paths, or security-sensitive logic.

Common Pitfalls

One common mistake is parsing the whole URL with ad hoc string operations instead of using built-in URL parsing utilities. That works until encoding, repeated keys, or missing values appear.

Another issue is forgetting that query parameters are strings. "2" and 2 are not the same thing in most programming languages.

Developers also sometimes read the fragment after # and assume it was sent to the server. In normal HTTP requests, it was not.

Finally, repeated parameters can surprise people. If your API allows the same key more than once, choose parsing functions that preserve that structure instead of silently dropping later values.

Summary

  • URL parameters usually mean the query string after ?.
  • In browser JavaScript, use URLSearchParams.
  • On the server, use your language's URL parser instead of manual string splitting.
  • Query parameters are strings, so convert types explicitly when needed.
  • Handle missing keys, repeated keys, and untrusted input carefully.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions