Variable is initialized without document.getElementById()? | Question regarding JavaScript behavior
I just finished the JavaScript Course and played a little around with the simple Image Slider you create during the Course. I recognized that as long as the JavaScript variable has the same name as the Id of the html tag the variable is automatically initialized(?) and I can change the attributes of the html tag. Example: https://code.sololearn.com/WCYoDeOwhxtp/#js I comment out the slider variable. But the assignment of the new Image still works. Worked with Google Chrome and Microsoft Edge. Why does JavaScript behave like this? Edit 1: So, I read a little bit more about JavaScript and found out that if I don't write var before a varible it will be still declared as a global variable. Sadly don't explain why the variable has a value instead of undefined