title: How to handle multiple formats for data in the same function in javascript
body: <p>I have a function that takes 2 pairs of latitude and longitude and returns a distance in m.</p>
  
  <pre><code>function distance(lat1, lon1, lat2, lon2){
        var p = Math.PI/180;
        var c = Math.cos;
        var a = 0.5 - c((lat2 - lat1) * p)/2 +
             c(lat1 * p) * c(lat2 * p) *
             (1 - c((lon2 - lon1) * p))/2;
  
         return 12742 * Math.asin(Math.sqrt(a)) * 1000; // 2 * R; R = 6371 km
      }//Distance
  </code></pre>
  
  <p>(note I didn't create this function but got it from another question on SO)</p>
  
  <p>I have multiple sources for latitude and longitudes and I would like to know how to handle them. I tried using <code>||</code> like this</p>
  
  <pre><code>var distanceFromTarget = distance(
        target.latitude || target.lat || target.lat(),
        target.longitude || target.lng || target.lng(),
        current.latitude || current.lat || current.lat(),
        current.longitude || current.lat || current.lng()
      );
  </code></pre>
  
  <p>I would like to use the function to handle input from three different formats </p>
  
  <pre><code>navigator.geolocation.x
  </code></pre>
  
  <p>which returns data in the format </p>
  
  <pre><code>{latitude: 0000, longitude:0000}
  </code></pre>
  
  <p>a plugin which returns </p>
  
  <pre><code>{lat: 000, lng: 000} 
  </code></pre>
  
  <p>and google maps api which returns a complex object for location containing getters for lat and lng something like this:</p>
  
  <pre><code>location = {lat(){return latitude}, lng(){return longitude}}
  </code></pre>
  
  <p>How can I handle all three? I thought what I have above was working but now I it is clear that it is not. Distance is returning NaN when the location is in the format <code>target.lat()</code>. Also it would be nice if someone could explain how <code>||</code> works in general since I thought it would just use the next object in line if the previous one was undefined.</p>