javascript get current location latitude and longitude

The getCurrentPosition() method is used to return the user’s position.

var x = document.getElementById("demo");
function getLocation() {
  if (navigator.geolocation) {
  } else {
    x.innerHTML = "Geolocation is not supported by this browser.";

function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude; 
Example explained: Check if Geolocation is supported If supported, run the getCurrentPosition() method. If not, display a message to the user If the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the parameter (showPosition) The showPosition() function outputs the Latitude and Longitude