Function createQueryString

  • Creates a query string from the given parameters.

    Parameters

    • params: { [key: string]: string | number | boolean }

      An object containing key-value pairs to be converted into a query string. The values can be of type string, number, or boolean.

    • OptionalsearchParams: null | URLSearchParams

      An optional instance of URLSearchParams to start with. If not provided, a new instance will be created.

    • Optionalencode: boolean

      An optional boolean indicating whether to encode the resulting query string using encodeURIComponent. Defaults to false.

    Returns string

    The generated query string.

    const params = { name: 'John', age: 30, active: true };
    const queryString = createQueryString(params);
    console.log(queryString); // Output: "name=John&age=30&active=true"

    const searchParams = new URLSearchParams('existing=param');
    const queryStringWithSearchParams = createQueryString(params, searchParams);
    console.log(queryStringWithSearchParams); // Output: "existing=param&name=John&age=30&active=true"

    const encodedQueryString = createQueryString(params, null, true);
    console.log(encodedQueryString); // Output: "name%3DJohn%26age%3D30%26active%3Dtrue"