In JavaScript, the URL object is a built-in global object that provides utilities for handling and manipulating URLs. It allows you to parse, construct, and modify URL strings more easily compared to manually working with strings.
What is a URL Object?
The URL object represents an entire URL, including its components like the protocol, host, pathname, search, and hash. It allows you to interact with these components programmatically.
Creating a URL Object
You can create a URL object by passing a string URL to the URL constructor.
This creates a URL object that allows you to access various parts of the URL using properties and methods.
Properties of the URL Object
The URL object provides several properties that represent the different components of the URL:
-
href: The entire URL (string representation). -
protocol: The protocol/scheme of the URL (e.g., 'http:', 'https:', 'ftp:'). -
host: The hostname and port (e.g., 'www.example.com:8080'). -
hostname: The hostname (without the port). -
port: The port number (e.g., '8080'). -
pathname: The path portion of the URL (e.g., '/pathname'). -
search: The query string (including the leading?), if present. -
searchParams: AURLSearchParamsobject that allows easy manipulation of query parameters. -
hash: The fragment identifier (e.g., '#hash'). -
usernameandpassword: If the URL contains authentication credentials, these properties hold the username and password.
Modifying URL Components
You can modify the URL components directly via the properties of the URL object:
Using URLSearchParams to Manipulate Query Parameters
The searchParams property is a URLSearchParams object, which provides methods for working with the query string of the URL (i.e., the part after the ?).
Common Methods for URLSearchParams:
-
get(name): Returns the first value for the given parameter name. -
set(name, value): Sets the value of a query parameter, replacing any existing value. -
append(name, value): Appends a new value for the given parameter name (if it already exists). -
delete(name): Deletes the query parameter. -
has(name): Checks if a query parameter exists. -
toString(): Returns the query string (the part after?).
Example: Working with URLSearchParams
URL Object Methods
-
toString(): Returns the entire URL as a string. -
toJSON(): Returns the URL as a string (same astoString()). -
origin: Returns the origin of the URL (i.e., the protocol, hostname, and port).
Practical Use Case: Creating URLs Dynamically
Suppose you're building a website where users can filter content with various query parameters. Using the URL object allows you to easily construct the URL with the necessary parameters.
Example: Dynamic URL Creation
Example: Validating URL
You can use the URL constructor to validate if a URL is well-formed.
Summary
- The
URLobject provides an easy way to work with and manipulate URLs in JavaScript. - You can create a
URLobject by passing a URL string to theURLconstructor. - It offers properties like
href,protocol,hostname,pathname,search, andhashfor accessing different components of a URL. - The
searchParamsproperty is aURLSearchParamsobject that allows for easy manipulation of the query string. - The
URLobject is useful for tasks such as validating, parsing, and dynamically generating URLs.

