Q:

Getting value outside for loop

I need to get values of Z variable outside a for but when I print it in the console from inside the loop it gives correct values, while I printing it from outside the loop, it gives one value of value that supposed to be returned
fetch('http://open.mapquestapi.com/elevation/v1/profile?key=tHXSNAGXRx6LAoBNdgjjLycOhGqJalg7&shapeFormat=raw&latLngCollection='+profile)
          .then(r => r.json()) 
          .then(data => {
            var Z;
            for(var i=0;i<data.elevationProfile.length;i++){
                //console.log(data.elevationProfile[i].height);
                Z = (data.elevationProfile[i].height);
                //console.log(Z);
                }
                console.log(Z);

A:

The reason you see only one value outside of the loop is because everytime you loop you assign a new var to Z with =
try setting the Z outside the loop as an array and inside the loop push variables to the array
later on you would be able to console your array with all the values
somthing like this:
fetch('http://open.mapquestapi.com/elevation/v1/profile?key=*CENCOREDKEY*&shapeFormat=raw&latLngCollection='+profile)
      .then(r => r.json()) 
      .then(data => {
        var Z=[];
        for(var i=0;i<data.elevationProfile.length;i++){
            //console.log(data.elevationProfile[i].height);
            Z.push(data.elevationProfile[i].height);
            //console.log(Z);
            }
            console.log(Z);

