URL

URL.prototype.origin

Returns a string containing the origin of the URL, which includes the scheme, domain, and port

Syntax

JavaScript
url.origin

Return Value

A string containing the URL origin (read-only)

Examples

Basic Usage
const url = new URL('https://example.com:8080/path')
console.log(url.origin) // 'https://example.com:8080'
Practical Example
function isSameOrigin(url1: string, url2: string): boolean {
  return new URL(url1).origin === new URL(url2).origin
}
Advanced Usage
const url = new URL('https://api.example.com/v1/users')
console.log(url.origin) // 'https://api.example.com'

Understanding URL.prototype.origin

The URL.prototype.origin method in JavaScript returns a string containing the origin of the URL, which includes the scheme, domain, and port. 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.origin. When called, it returns a string containing the url origin (read-only). Understanding when and how to use origin() helps you write more expressive, readable code.

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

Browser support for URL.prototype.origin 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.