javascript string to query string code example

Example 1: js object to querystring

const params = {foo:"Hi", bar:"there"}
const queryString = new URLSearchParams(params).toString();

console.log(queryString);

Example 2: javascript object to params string

var queryString = Object.keys(params).map(key => key + '=' + params[key]).join('&');

Example 3: js query string

const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');

Example 4: how to write query string js

//Using query string to concat variable with string
const txt = "My name is"
console.log(`${txt} Paul`);