How to Encode an Email in JavaScript for Use in the Query String?

Learn how to encode an email in JavaScript to use it as a GET Param Value

To use an email in the query string component of a URL, you can encode it using the encodeURIComponent() function in JavaScript. This would encode all characters except A-Z a-z 0-9 - _ . ! ~ * ' ( ). For example:

const email = 'foo.bar_baz+10@bar.com';
const encodedEmail = encodeURIComponent(email);
const url = `https://example.com/?email=${encodedEmail}`;

console.log(url); // https://example.com/?email=foo.bar_baz%2B10%40bar.com

Hope you found this post useful. It was published . Please show your love and support by sharing this post.