Search code examples
javascripthtmldebugginginputgetelementbyid

For some reason able to access value of input in js without document.getElementById


function buttonPress()
{
    var userInput=document.getElementById('userInput');
    alert(userInput.value);
}

I removed

`var userInput=document.getElementById('userInput');`

and was left with

`alert(userInput.value);` 

but the output did not change at all it was left the same, userInput is not even declared but it works. I cleaned all the data from browser,restarted the server, but result did not change, my brain is exploding, could it be because I am using the same name for id and the var?


Solution

  • In JavaScript, variables that are assigned a value but not declared are implicitly declared as a global variable.