I use a shorthand function to avoid having to type let x = document.getElementById('myId'); every time I want a reference to an element on the page:
function element(id) {
return document.getElementById(id);
}
Now I can just use the much simpler:
let x = element("id");
Today, by accident, I did this:
let x = element("id").value;
And it worked, and actually contained the value of the HTML element "id".
But my element() function doesn't provide a .value property. It simply returns a reference to an element.
It should have caused an error of some type. Why didn't it? Or why did it work?
You've accessed a .value property of a function result, not of a function itself.
element.value // undefined, gets value property of a function
element(id).value // gets an element by id and returns it's value