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);