I'm working on a website currently, and something strange is going on.
I have this code:

CnP version:
var gameID = location.href.split("&gameID=")[1]
var quizID
firebase.database().ref('liveGames/' + gameID).get().then((snapshot) => {
quizID = snapshot.val().quizID
})
console.log("Quiz ID: " + quizID)
When I try to run that, the console logs Quiz ID: undefined
.
But when I put it in a different spot as shown below,

The console says the quiz ID properly. (It says Quiz ID: 7630579719
)
Also, if I put it in either spot, DevTools says the correct thing as well:


Why does this all happen, and how can I fix it to never say undefined
and always say the right one?
Additional details:
- I do have Firebase initialize app and credentials, etc.
- I'm using Firebase version 8.4.3 (I find the modular v9 too hard to comprehend)
If you need any additional details, please ask in replies. Thanks a bunch!
Bost