How to get the domain name from a URL in Javascript?


I want to get the domain name of the current URL using JS.

for example,

If the URL is https://onecompiler.com/questions/create

I want the output as onecompiler.com

How can I do that?

1 Answer

4 years ago by

To get the domain name, you can use the document object

console.log(document.domain);

output will be "onecompiler.com"

4 years ago by Divya