How to get current URL Location using Javascript HTML

Ever came across a requirement where you need to grab the current page URL from JavaScript. See below examples to learn how to achieve this and some other parts of URL with the help of window.location object. You can either use the code directly or save it in a variable and then use.

Code Type : Get entire Location (URL)
var url = window.location.href;
Code Type : Get Location HostName
var hostName = window.location.hostname;