The getElementById method returns an Element that can be used to interact with an element. If the item is not found, null returned. In the case of an input element, the value property of the object contains a string in the value attribute.
Using the fact that the short circuits of the && operator and that both null and the empty string are considered βfalseβ in a Boolean context, we can combine checks for the existence of an element and the presence of value data as follows:
var myInput = document.getElementById("customx"); if (myInput && myInput.value) { alert("My input has a value!"); }
source share