0

In Javascript, URLSearchParams Is it different "set" between "append"?

const test = new URLSearchParams();
test.append("name", "Harry Potter");
const test = new URLSearchParams();
test.set("name", "Harry Potter");

^ different?

Esteban
  • 15
  • 3
  • Does this answer your question? [What is the difference between the set and append methods of angulars HttpParams object?](https://stackoverflow.com/questions/51980630/what-is-the-difference-between-the-set-and-append-methods-of-angulars-httpparams) – Seaweed1603 Jul 10 '22 at 11:44
  • By append, the parameter will add whether it existed, but by set, it will add or edit if exist – Parsa S Jul 10 '22 at 11:46
  • To be clear should this be tagged 'Angular", and if so, which precise tag, asking for a Friend – Dexygen Jul 10 '22 at 11:55

1 Answers1

1

The difference becomes easy to see when you call the methods twice. With append, both values are included in the URL, while with set, any present value will be overridden.

const url = new URLSearchParams()
url.append("name", "Harry Potter")
url.append("name", "Hermione Granger")
url.toString() // "name=Harry+Potter&name=Hermione+Granger"
const url = new URLSearchParams()
url.set("name", "Harry Potter")
url.set("name", "Hermione Granger")
url.toString() // "name=Hermione+Granger"
CerebralFart
  • 3,336
  • 5
  • 26
  • 29