---
# System prepended metadata

title: 1. URL Parameters

---

<!-- JSD7G30 -->
<!-- url_search_params -->

### Q. Which function is used to grab URL parameters?

_**Rephrased Question:** How would you grab the UTM parameters?_

_**Intent of the interview:** To understand the candidate’s knowledge of function to retrieve URL parameters using JavaScript.


`URLSearchParams` function is used to grab URL parameters.


In JavaScript, One common way to grab the query parameters in URL is by using the `URLSearchParams` function.

<details>
<summary>More Info</summary>
<br/>

The `URLSearchParams` allows you to work with the query parameters in a URL. Here's an example:

```js
const params = new URLSearchParams(window.location.search);

let myParam = params.get("myParam"); // If the URL is example.com?myParam=value, this will return 'value'

console.log(myParam);
```

This method works for all query parameters.

**UTM parameters:** These are specific query parameters used by marketers to track the effectiveness of online campaigns across traffic sources and publishing media. They usually begin with "utm\_" like utm_source, utm_medium, etc.

```js
// https://example.com/page?utm_source=facebook&utm_medium=cpc&utm_campaign=spring_sale
const utmParams = new URLSearchParams(window.location.search);
let source = utmParams.get("utm_source"); // This will return 'facebook'
let medium = utmParams.get("utm_medium"); // This will return 'cpc'
let campaign = utmParams.get("utm_campaign"); // This will return 'spring_sale'
```

</details>

-----

#### NXT MOCK CONTENT

### Q. Which function is used to grab URL parameters?

### Answer

In JavaScript, One common way to grab the query parameters in URL is by using the `URLSearchParams` function.

-----

#### QUESTIONS

@QUESTION_KEY: 1
@QUESTION_ID: 35e2d0f5-9cf1-4c52-a9d4-6d2d11a4b1a2
@QUESTION_TYPE: MULTIPLE_CHOICE

@QUESTION:

What is the primary use of the `URLSearchParams` class in JavaScript?

@QUESTION_CONTENT_TYPE: MARKDOWN

@OPTION1: To parse and manipulate the path of a URL.
@OPTION2: To send HTTP requests to a server.
@OPTION3: To handle query parameters in a URL.
@OPTION4: To encode and decode URL strings.
@CORRECT_OPTIONS: OPTION3
@TAG_NAMES: url_search_params

---END

@QUESTION_KEY: 2
@QUESTION_ID: b4de3f7b-dbc8-4eab-b759-5fa9aef57b52
@QUESTION_TYPE: MULTIPLE_CHOICE

@QUESTION:

For a URL `http://example.com/?item=book&price=20`, what will be the output of the below code?

```js
let urlParams = new URLSearchParams(window.location.search);
let itemValue = urlParams.get("item");
console.log(itemValue);
```

@QUESTION_CONTENT_TYPE: MARKDOWN

@OPTION1: 20
@OPTION2: book
@OPTION3: price
@OPTION4: item
@CORRECT_OPTIONS: OPTION2
@TAG_NAMES: url_search_params

---END

@QUESTION_KEY: 3
@QUESTION_ID: 2cb86d56-4d02-40f1-9f33-5ef283ab1a21
@QUESTION_TYPE: MULTIPLE_CHOICE

@QUESTION:

If a query parameter does not exist in a given URL, what will the `get` method of `URLSearchParams` return for that parameter?

@QUESTION_CONTENT_TYPE: MARKDOWN

@OPTION1: An empty string ("")
@OPTION2: The string "undefined"
@OPTION3: The value `null`
@OPTION4: The value `undefined`
@CORRECT_OPTIONS: OPTION3
@TAG_NAMES: url_search_params

---END

@QUESTION_KEY: 4
@QUESTION_ID: 6f49f773-e979-4c1a-b438-a3905a26098e
@QUESTION_TYPE: MULTIPLE_CHOICE

@QUESTION:

Which of the following describes the purpose of UTM parameters?

@QUESTION_CONTENT_TYPE: MARKDOWN

@OPTION1: Encrypting the URL for secure transmission.
@OPTION2: Storing session data for web applications.
@OPTION3: Tracking the source and performance of marketing campaigns.
@OPTION4: Redirecting users to a different domain or path.
@CORRECT_OPTIONS: OPTION3
@TAG_NAMES: url_search_params

---END

@QUESTION_KEY: 5
@QUESTION_ID: 3ef2e52e-593c-4356-95d1-d1dc40c1b282
@QUESTION_TYPE: MULTIPLE_CHOICE

@QUESTION:

Which class in JavaScript is used to grab the UTM or any query parameters?

@QUESTION_CONTENT_TYPE: MARKDOWN

@OPTION1: `URLParser`
@OPTION2: `QueryString`
@OPTION3: `URLParameters`
@OPTION4: `URLSearchParams`
@CORRECT_OPTIONS: OPTION4
@TAG_NAMES: url_search_params

---END


