JavaScript Example | Get URL Parameters

This javascript example code will show us how to get url parameters correctly.

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the Document</title>
  </head>
  <body>
    <script>
      const urlParams = new URLSearchParams("https://example.com/?product=troussers&color=black&newuser&size=s");
      alert(urlParams.getAll('color'));
      // [ 'black' ]
      //Programmatically add a second color parameter.
      urlParams.append('color', 'pink');
      alert(urlParams.getAll('color'));
      // [ 'black', 'pink' ]
    </script>
  </body>
</html>