URL

URL.prototype.pathname

Returns or sets the path section of the URL, which comes after the host and before the query string

Syntax

JavaScript
url.pathname

Return Value

A string containing the URL pathname

Examples

Basic Usage
const url = new URL('https://example.com/api/users/123')
console.log(url.pathname) // '/api/users/123'
Practical Example
const url = new URL('https://example.com/old-path')
url.pathname = '/new-path'
console.log(url.href) // 'https://example.com/new-path'
Advanced Usage
function getPathSegments(urlStr: string): string[] {
  const url = new URL(urlStr)
  return url.pathname.split('/').filter(Boolean)
}

Understanding URL.prototype.pathname

The URL.prototype.pathname method in JavaScript returns or sets the path section of the URL, which comes after the host and before the query string. It belongs to the URL object and is one of the most widely used methods for working with url values in modern JavaScript and TypeScript applications.

The method signature is url.pathname. When called, it returns a string containing the url pathname. Understanding when and how to use pathname() helps you write more expressive, readable code.

Common use cases for URL.prototype.pathname include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like url-hostname, url-search, url-hash, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for URL.prototype.pathname is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More URL Methods

Other methods in the URL object

Related Tools

More URL Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.