URLSearchParams.prototype.set
Sets the value associated to a given search parameter to the given value, removing others with the same name
Syntax
params.set(name, value)Parameters
| Parameter | Type | Description |
|---|---|---|
| name | string | The name of the parameter |
| value | string | The value to set |
Return Value
undefined
Examples
const params = new URLSearchParams('page=1')
params.set('page', '2')
console.log(params.toString()) // 'page=2'const url = new URL(window.location.href)
url.searchParams.set('view', 'grid')
history.pushState({}, '', url)function setQueryParam(name: string, value: string) {
const url = new URL(window.location.href)
url.searchParams.set(name, value)
return url.toString()
}Understanding URLSearchParams.prototype.set
The URLSearchParams.prototype.set method in JavaScript sets the value associated to a given search parameter to the given value, removing others with the same name. It belongs to the URLSearchParams object and is one of the most widely used methods for working with urlsearchparams values in modern JavaScript and TypeScript applications.
The method signature is params.set(name, value). It accepts 2 parameters: name, value. When called, it returns undefined. Understanding when and how to use set() helps you write more expressive, readable code.
Common use cases for URLSearchParams.prototype.set include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like urlsearchparams-get, urlsearchparams-append, urlsearchparams-delete, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for URLSearchParams.prototype.set is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
URLSearchParams.prototype.getReturns the first value associated to the given search parameter
URLSearchParams.prototype.appendAppends a specified key/value pair as a new search parameter, without removing existing ones with the same name
URLSearchParams.prototype.deleteRemoves the given search parameter and all its associated values
More URLSearchParams Methods
Other methods in the URLSearchParams object
Related Tools
More URLSearchParams Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.