JavaScript replace URL parameter value | Example code

  • by

Use searchParams API with a set method to replace URL parameter value in JavaScript.

Example code change URL parameter value in JS

HTML example code if the parameter is the only parameter.

Example 1

Override parameter value.

var href = new URL('');
href.searchParams.set('q', 'dogs');
console.log(href.toString()); //


JavaScript replace URL parameter value

Example 2

If Multiple parameters are present in the URL and update only the parameter values.



		let url = new URL('');
		let params = new URLSearchParams(;

		url.searchParams.set('foo', 'baz');



Do comment if you have any doubts or 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.