Here is my code
var form = document.querySelector('form');
var name = form.querySelector('div #name');
form.addEventListener('submit', function(e) {
e.preventDefault();
console.log(name.value); // undefined
})
<form>
<div>
<input id="name" />
</div>
<div>
<input type="submit" id="submit" />
</div>
</form>
console.log prints undefined if I use var when declaring name. But prints the actual value if I use const instead of var.
const name = form.querySelector('div #name');
I guess it's a hoisting problem, but can't understand why it's happening. Anyone please explain why it's not working while using var.