Getting value outside (for) loop : I need to get values of Z variable outside a `for` but when i print it in 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);