For some reason the variables set in my functions only apply to the scope of the function, even with window in front of the declaration:
<body>
<input id="usernameBox" placeholder="Name">
<input id="ageBox" placeholder="Age">
<br>
<button onclick="changeUsername(), changeAge()"id="niceButton">Confirm</button>
<script>
var username;
var age;
function changeUsername(){
window.username=document.getElementById("usernameBox").value
console.log(username)
}
function changeAge(){
window.age=document.getElementById("ageBox").value
console.log(age)
}
console.log(username);
</script>
The final console log is supposed to log what's inserted in the text box "usernameBox", but it just prints undefined, even after the button is clicked. I even added a checker within the functions, and they properly log what's inside the text boxes when clicked, so why is username still undefined outside of the function?