Url parameters

The URLSearchParams interface defines utility methods to work with the query string of a URL.

// for example ?param1=val1&param2=val2
const queryString = window.location.search;

const urlParams = new URLSearchParams(queryString);
const param2 = urlParams.get('param2'); // get second parameters
method description
append(name, value) Appends a specified key/value pair as a new url parameter.
delete(name) Deletes the given parameter.
entries() Returns an iterator allowing iteration through all key/value pairs contained in this object.
forEach(callback) Allows iteration through all values contained in this object via a callback function. The callback function has two paramters: value and key.
params.forEach(function(value, key) {
  console.log(value, key);
});
get(name) Returns the first value associated to the given parameter.
getAll(name) Returns all the values associated with a given parameter as an array.
has(name) Returns a Boolean that indicates whether a parameter with the specified name exists.
keys() Returns an iterator allowing iteration through all keys contained in this object.
set(name, value) Sets the value associated with a given parameter to the given value. If there were several matching values, this method deletes the others. If the parameter doesn't exist, this method creates it.
sort() Sorts all key/value pairs contained in this object.
toString() Returns a query string suitable for use in a URL without ?.
values() Returns an iterator allowing iteration through all values contained in this object.
for(var value of params.values()) {
  console.log(value);
}