Categories
JavaScript Answers

How to perform a DNS lookup from hostname to IP address using client-side JavaScript?

Spread the love

Sometimes, we want to perform a DNS lookup from hostname to IP address using client-side JavaScript.

In this article, we’ll look at how to perform a DNS lookup from hostname to IP address using client-side JavaScript.

How to perform a DNS lookup from hostname to IP address using client-side JavaScript?

To perform a DNS lookup from hostname to IP address using client-side JavaScript, we can use a 3rd party API.

For instance, we write

const response = await fetch("https://dns.google/resolve?name=example.com");
const json = await response.json();
console.log(json);

to call fetch in an async function to make a get request to https://dns.google/resolve with the name query parameter set to the hostname we want to resolve.

Then we get the IP address from the JSON response we get from response.json.

Conclusion

To perform a DNS lookup from hostname to IP address using client-side JavaScript, we can use a 3rd party API.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *