How to Check for a Hash Value in a URL Using JavaScript

The web page elements are modified depending on thehash (#) value of the current URL. You can get the hash value from the URL quickly by just running JavaScript code.

You can use the window.location.hash method to get the hash:

Javascript get hash using window.location.hash method
let hash = window.location.hash; console.log("window.location.hash : " + hash);
Javascript get hash using window.location.hash method
let hash = window.location.hash; if (window.location.hash) { // Fragment exists console.log("window.location.hash : " + hash); } else { // Fragment doesn't exist console.log("no window.location.hash" ); }

The hash property contains the fragment identifier, including the hash ‘#’ sign for the current page.

The location hash property returns the fragment identifier, including the hash ‘#’ sign for the current page. For example:

http://w3docs.com/#foo

Here, the location hash property would return the fragment identifier of #foo.

The window.location Property

The location interface represents the URL of the object to which it is. The changes that are done on it are reflected in the object to which it relates. The window.location is a reference to a location object representing the current URL of the document being displayed in that window.