Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

URL.hash — the URL fragment

URL.hash reads and sets a URL fragment. Learn its encoding behavior and the distinction between empty and absent fragments.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

url.hash
url.hash = fragment

URL.hash is a writable instance property of URL, not a static method. It returns the fragment with a leading #, or "" for an empty or absent fragment.

Assignment accepts text with or without a leading #. Characters requiring it are percent-encoded; reading does not decode them. A space becomes %20.

url.hash = '' removes the fragment. url.hash = '#' preserves an empty fragment: url.hash still returns "", but url.href ends in #. This is a Web API from the WHATWG URL Standard, not an ECMAScript-defined feature.

Examples

const url = new URL('https://example.org/');
url.hash = 'part one';
console.log(url.hash);
url.hash = '#';
console.log(JSON.stringify(url.hash), url.href);
url.hash = '';
console.log(url.href);

Result

#part%20one
"" https://example.org/#
https://example.org/

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources