JavaScript Check whether a URL string is absolute or relative Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, the task is to check if the passed URL is absolute or relative. Below are a few approaches: Approaches to Check if URL is Absolute or Relative:Table of Content Approach 1: Using JavaScript Regular ExpressionApproach 2: Using JavaScript string.indexof() methodApproach 3: Using JavaScript URL Constructor Approach 1: Using JavaScript Regular ExpressionUse Regular Expression which checks if the URL contains "//" at a position in the URL. Example: This example uses the approach discussed above. JavaScript // Input url let URL = "https://www.geeksforgeeks.org/"; // Display input url console.log(URL); // Function to check url function gfg_Run() { // Regular Expression to check url let RgExp = new RegExp("^(?:[a-z]+:)?//", "i"); if (RgExp.test(URL)) { console.log("This is Absolute URL."); } else { console.log("This is Relative URL."); } } // Function call gfg_Run(); Outputhttps://www.geeksforgeeks.org/ This is Absolute URL.Approach 2: Using JavaScript string.indexof() methodUse .indexOf() method to get to know if the position of "://" has an index greater than 0 or if the position of "//" has an index equal to 0. Both these conditions check to lead us to the absolute URL. Example: This example uses the approach discussed above. JavaScript // Input url let URL = "/myfolder/test.txt"; // Display input url console.log(URL); // Function to check url type function gfg_Run() { // Using index of to check url format if (URL.indexOf("://") > 0 || URL.indexOf("//") === 0) { console.log("This is Absolute URL."); } else { console.log("This is Relative URL."); } } // Function call gfg_Run(); Output/myfolder/test.txt This is Relative URL.Approach 3: Using JavaScript URL ConstructorThe URL constructor can be used to parse the given URL. If the constructor throws an error, it means the URL is relative. If it parses without an error, it's an absolute URL. Example: This example uses the approach discussed above. JavaScript // Input url let urlString = "https://www.example.com/images/logo.png"; // Display input url console.log(urlString); // Function to check url type function checkURLType(urlString) { try { new URL(urlString); console.log("This is Absolute URL."); } catch (_) { console.log("This is Relative URL."); } } // Function call checkURLType(urlString); Outputhttps://www.example.com/images/logo.png This is Absolute URL. Comment More infoAdvertise with us Next Article How to check a URL contains a hash or not using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More Similar Reads How to check whether a given string is an absolute URL or not in JavaScript? We will learn how to return true if the given string is an absolute URL in JavaScript. There are two types of URLs either relative or absolute.Absolute URL: It is a URL that contains all the information that is important for locating the resources. The thing that is included in the absolute URL is t 3 min read JavaScript - How To Check Whether a String Contains a Substring? Here are the different methods to check whether a string contains a substring in JavaScript.1. Using includes() MethodThe includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false oth 3 min read Convert relative path URL to absolute path URL using JavaScript Given a relative URL, the task is to convert the relative URL to an absolute URL. Here, the base URL is also given. 2 approaches are discussed here, the first example has the baseURL provided by the user and the second takes it from the URL of the page. Approach 1: Get the relURL and baseURL from us 3 min read How to check a URL contains a hash or not using JavaScript ? In this article, the task is to check whether an URL contains or not. This can be done by using the Location hash property in JavaScript. It returns the string which represents the anchor part of a URL including the hash â#â sign. Syntax: window.location.hash Example: This example uses the hash prop 1 min read How To Get URL And URL Parts In JavaScript? In web development, working with URLs is a common task. Whether we need to extract parts of a URL or manipulate the URL for navigation, JavaScript provides multiple approaches to access and modify URL parts. we will explore different approaches to retrieve the full URL and its various components.The 3 min read How to parse a URL into hostname and path in javascript ? We can parse a URL(Uniform Resource Locator) to access its component and this can be achieved using predefined properties in JavaScript. For Example, the first example parses the URL of the current web page and the second example parses a predefined URL. Example 1:This example parses the URL of the 1 min read Like