10 August 2024

To add a path to an existing URL in JavaScript, you can use the URL object and its methods.

Source code viewer
  1. // Example URL
  2. let urlString = "https://example.com/path/to/resource";
  3.  
  4. // Create a new URL object
  5. let url = new URL(urlString);
  6.  
  7. // Extract the path
  8. let path = url.pathname;
  9.  
  10. console.log(path); // Output: "/path/to/resource"
Programming Language: Javascript