Node | URL.hash API
The url.hash is an inbuilt application programming interface of class URL within url module which is used to get and set the fragment portion of the URL.
Syntax:
url.hash
Return value: It gets and sets the fragment portion of the URL.
Below programs illustrate the use of url.hash Method:
Example 1:
Javascript
// node program to demonstrate the// url.hash API as Setter// creating and initializing myURL// Display href value of myURL before changeconsole.log("Before Change");console.log(myURL.href);// assigning fragment portion// using hashconsole.log();myURL.hash = 'rahim';// Display href value of myURL after changeconsole.log("After Change");console.log(myURL.href); |
Output:
Before Change https://example.org/foo#ram After Change https://example.org/foo#rahim
Example 2:
Javascript
// node program to demonstrate the// url.hash API as Getter// creating and initializing myURL// getting the fragment portion// using hashconst hash = myURL.hash;// Display hash valueconsole.log(hash); |
Output:
#ram
NOTE: The above program could be run by using the node myapp.js command.
Reference:
https://nodejs.org/dist/latest-v10.x/docs/api/url.html#url_url_hash


