JavaScript adds a parameter to URL | Example code

  • by

Use URL.searchParams method with append method to add a parameter to URL in JavaScript. You can also set method.

JavaScript adding a parameter to URL Examples

HTML Example code:

Using append Method

To add a parameter that has multiple values use the append() method.



		var url = new URL("");

		url.searchParams.append('param_1', 'val_1');



JavaScript adds a parameter to URL

More examples

var url = new URL("");

// If your expected result is ""
url.searchParams.append('x', 42);

// If your expected result is ""
url.searchParams.set('x', 42);

Using set method

The value of a parameter can be updated with the set() method of URLSearchParams object.

		var url = new URL("");

		url.searchParams.set('param_1', 'val_1');


Do comment if you have any doubts and suggestions on this JS URL topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.