Exercise 15: Check if String is Valid URL

Problem Statement

Function `isValidURL(str)` banao jo check kare ki string valid URL hai ya nahi. Example: isValidURL("https://example.com") => true

Sample Output:

isValidURL("https://example.com") => true
isValidURL("not-a-url") => false

Solution

const isValidURL = (str) => {
  try { new URL(str); return true; }
  catch { return false; }
};

Explanation

Overall Goal:

  • String valid URL hai ya nahi check karna.

Real world:

  • Form validation: URL inputs.
  • Link validation: URL checks.