JavaScript URLSearchParams toString () Tutorial

In this section, we will learn what the toString() method is and how to use it in JavaScript.

JavaScript URLSearchParams toString () Method

The `toString()` method is used to return the target query in a format that can be used in a URL.

Note: the question mark is removed from the query.

URLSearchParams toString() Method Syntax:

URLSearchParams.toString()

URLSearchParams toString () Method Parameter:

The method does not take an argument.

URLSearchParams toString () Method Return Value:

The return value of this method is a string value suitable to be used in a URL.

Example: using URLSearchParams toString() method in JavaScript

const searchParams = new URLSearchParams("?name=John&lastName=Doe&address=Mars&name=Omid&[email protected]");

console.log(searchParams.toString());

Output:

name=John&lastName=Doe&address=Mars&name=Omid&email=john%40example.com

Leave a Reply