const urlParams = new URLSearchParams(window.location.search); const name = urlParams.get('name'); console.log(name); // Output: "John"
const urlParams = new URLSearchParams(window.location.search); const colors = urlParams.getAll('color'); console.log(colors); // Output: ["red", "green", "blue"]
const urlParams = new URLSearchParams(window.location.search); if (urlParams.has('page')) { const page = urlParams.get('page'); console.log(page); // Output: "2" } else { console.log('Page parameter not found!'); }In this example, we use the has method of URLSearchParams to check if the 'page' parameter exists in the URL. If it does, we extract its value using the get method. This functionality is built into JavaScript and does not require any additional package/library.