HOWTO compute distance between 2 addresses using Google Maps API

I recently had a requirement to figure out the distance between an address and a zip code (or it could be an address too). I figured that this is best solved by using Google Maps API. If you are curious on how to do that, read on.
There are couple of ways to get the distance. The first way is the straight line between 2 points (I'm assuming it's flying distance). The second way is the distance between 2 points if you were traveling by car.
Regardless of the way, you need to load a script for JavaScript API:

   src="<a href="">


Then you'll probably will want a form where your user will input start address and end address like so:


Then, once the form is ready, you can write the code to get the distance...
Since I'm going to describe both approaches, let's make the 'getDistance()' method to be a generic method that gets both distances

function getDistance() {
  startLoc = document.forms[0].address1.value;
  endLoc = document.forms[0].address2.value;
  findDistanceFromGeocoder(startLoc, endLoc);
  findDistanceFromAddresses(startLoc, endLoc);

Now that we have our 'main' (so to speak), let's define the missing pieces.

findDistanceFromGeocoder function

Google Maps API implements an object called Geocoder, which has to created before usage like so

var geocoder = new google.maps.Geocoder();

Next, we have to get the geocoded location for both start and end positions like so:

var loc1, loc2;
geocoder.geocode({'address': startLoc}, function (result, status) {
  // make sure that our request went through OK
  if (status === google.maps.GeocoderStatus.OK) {