I faced this problem when i doing my project and not able to use addEventListener function. So I make new file and try to catch the error, so basically its store null value to the variable that's why i am not able to use. When i use the same code on the console , that was executed and give the expected result but not with VS code.
*JS code
let elem7=document.getElementById("mainDiv")
console.log(elem7)//prints null
elem7.addEventListener('click',function(e){
console.log("You Clicked")
})
HTML CODE:
<body>
<h3>Editable div</h3>
<div id="mainDiv">
</div>
</body>