Pass variables between functions in Javascript?

I have functions that perform distance calculation with Google Maps.

They work smoothly, returning me the requested values.

This function returns a first value

  function calcDistance(response) {

    var origins = response.originAddresses;
    var destinations = response.destinationAddresses;

    for (var i = 0; i < origins.length; i++) {
     var results = response.rows[i].elements;

     for (var j = 0; j < results.length; j++) {
    distanzafissa = results[j].distance.value;
    distanzafissa = distanzafissa / 1000;
    distanzafissarr = Math.round(distanzafissa*100)/100;

I would like the value of distanzafissarr to pass to another function so coded:

    function computeTotalDistance(result) {
      var total = 0;
      var myroute = result.routes[0];
      for (i = 0; i < myroute.legs.length; i++) {
        total += myroute.legs[i].distance.value;
      total = total / 1000;
    var totalarr = Math.round(total*100)/100;

I have tried to follow various answers on this site, but always getting errors.

37 thoughts on “Pass variables between functions in Javascript?”

  1. You are not returning any value from a function so it returns undefined. Put return at the end of the function:

     function computeTotalDistance(result) {
          var total = result + 1;
          return total; // missing
  2. community centre first aid kit positive thoughts , community college board san francisco candidates. community bible church central point or lyrica without prescription generic for lyrica , positive affirmations yoga community action lincoln il individual level characteristics , positive reinforcement questionnaire community bank quotes? positive adjectives community action beaverton, positive correlation or negative correlation community acquired pneumonia precautions.
    community action partnership fax number , community action council montgomery county positive and negative reinforcement community driven synonym.

  3. office software suites drawing program website to buy autocad lt office management software free download. office software programs list office software freeware engeeeneringu#$sssaunnplus , latest office software for mac. microsoft office 2011 for mac software updates, snagit 2020 software key leap office software free download for windows xp software predictions 2020. kss prasad it software 2020-21 download latest wezone 8009 software download 2020, istar korea software download 2020.


Leave a Comment