I'm experiencing some weird behaviour in JS that I've never experienced before. I'm trying to make an API call to get a users profile, however, the variable doesn't seem to get defined. This is my API call
axios.get('http://127.0.0.1:8000/api/v1/profile/',{
  headers: {
    Authorization: `Token ${token}`
  }
}).then(response => {
  var userProfile = response.data
  console.log(userProfile)
})
Everything above works normally fine, however, when I try to access the variable userProfile the chrome console says it's undefined. For example the following code gives an undefined error:
axios.get('http://127.0.0.1:8000/api/v1/profile/',{
  headers: {
    Authorization: `Token ${token}`
  }
}).then(response => {
  var userProfile = response.data
})
console.log(userProfile)
I thought that declaring a variable with var I could access it outside the function call and I even defined userProfile = null initially and then tried changing it but the value didn't seem to change. Any help would be appreciated.
