Get link to anchor (hash) with JavaScript

How to get the hash part (the part of the URL after the # symbol) with JavaScript:

The string with the # sign

var hash = window.location.hash;

The string without the # sign

var hash = window.location.hash.substring(1);