
$(document).ready(function () {
let degree
fetch("http://spartacodingclub.shop/sparta_api/weather/seoul").then(res => res.json()).then(data => {
degree = data['temp']
console.log("1: " + degree)
$('#temp').text(degree)
})
console.log("2: " + degree)
})
degree 변수가 살아있는 범위가 어디인가요? 저는 1: 15.67, 2: 15.67 이라고 나와야 된다고 생각하는데, 2:에는 null 값이 들어가는 것처럼 보이네요.
저는 degree를 $(document).ready(function () 함수 내에 선언했기 때문에, degree를 $(document).ready(function () 내에서 메모리에 살아있다고 생각했습니다. 그런데 콘솔에 찍어보면, 아래 처럼 나오네요.
2: undefined
1: 15.67
