I'm a beginner in js and while experimenting I noticed something strange in my app :
var pin = "";
var inputField = document.querySelector('.pin'); // initialize globally
const addNum = (num)=>{
//let inputField = document.querySelector(".pin"); //code works by initializing this
pin+=num;
inputField.value = pin; //error hits
}
If I initialize my inputField variable globally I get the error Uncaught TypeError: Cannot set property 'value' of null
in my app . However when I declare and initialize the variable in my function I get no null error and my code runs . Why does this happen if my variable is globally scoped how can it be null?