javascript ip address

Javascript IP Address

If you want to get the IP address of the client using Javascript, there are few ways to do it.

Method 1: Using a third-party service

One way to get the IP address is by using a third-party service like This service provides a REST API that returns various information about the client, including their IP address.

  .then(response => response.json())
  .then(data => console.log(data.ip))

The above code fetches the JSON data from the URL and logs the client's IP address to the console.

Method 2: Using WebRTC

Another way to get the IP address is by using WebRTC. WebRTC is a technology that enables real-time communication between browsers and mobile applications. It can also be used to get the client's IP address.

const peerConnection = new RTCPeerConnection();
  .then(offer => peerConnection.setLocalDescription(offer))
  .then(() => {
    const lines = peerConnection.localDescription.sdp.split('\n');
    const line = lines.find(l => l.startsWith('a=candidate'));
    const candidate = line.split(' ')[4];
    const [host, port] = candidate.split(':');

The above code creates an RTCPeerConnection object and creates a dummy data channel to trigger ICE candidate gathering. It then creates an offer and sets it as the local description of the connection. Finally, it parses the SDP to extract the client's IP address.

Method 3: Using a server-side script

One more way to get the IP address is by using a server-side script. You can make an Ajax request to a server-side script that returns the client's IP address.

$.get('', function(data) {

The above code makes an Ajax request to the get-ip-address.php script on the domain and logs the response to the console.

These are some of the ways you can get the IP address of the client using Javascript.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]