You can use URLSearchParms and FormData(formELement) which are compatible with most modern browsers you can do it like this:
new URLSearchParams(new FormData(formElement)).toString()
If you want it to be compatible with older browsers like IE10 then you have to use Array. from like this:
Array.from(
new FormData(formElement),
e=>e.map(encodeURIComponent).join(‘=’)).join(‘&’)
Want to be a web developer? Check out the full stack developer course from Intellipaat.