As per my understanding and tiny experience, document.getElementById('element'); will return the element if it exists otherwise return null, if it does not exist. so in any case it would not return undefined I wish to confirm these from experts, therefore asking here to expert's community Thank you
-
2No, it does not return `undefined`, it's either `null` or an element – adeneo Jan 29 '15 at 07:22
-
If you actually need to fetch the status of "undefined" from something in the overall document, then you can assign some sort of ID to one of the ` – vernonner3voltazim Jan 29 '15 at 07:57
2 Answers
No, it will always return null
or a DOM element. It cannot return undefined
.
See the W3C DOM spec:
Returns the
Element
that has an ID attribute with the given value. If no such element exists, this returnsnull
.
Note that the spec also says this:
If more than one element has an ID attribute with that value, what is returned is undefined.
That doesn't mean it will return undefined
, it simply means that invoking getElementById
when more than one element has a given ID invokes undefined behavior. However, no implementations would return anything other than an element or null
even in that case.

- 43,109
- 15
- 131
- 205
-
Yes Sir, I checked one of its test casehibye. now document.getElementById('element'); returns hi. >> So may be it returns the first element it encounters with that id. – Akshay Vijay Jain Jan 29 '15 at 09:15
As described in a comment above:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Test Page</title>
<script type="text/javascript" id="UN">
//<!--
var a, b, undef;
function testthis()
{ a=document.getElementById("UN");
//COULD now do: b=a.undef;
b=document.getElementById("UN").undef;
//If you set a breakpoint where you can examine b
// its value will be undefined
return;
}
//-->
</script>
</head>
<body onload="testthis();">
test page
</body>
</html>
It could be pointed out that we are dealing with OBJECTS; the document.getElementById()
function will return null
if the ID cannot be found (no object has that ID), and will return an object if the ID can be found. There is no such thing as an object that simultaneously has an ID yet is otherwise "undefined" --although as this test-code shows, an object may contain something (like that undef
variable) which is undefined.

- 780
- 6
- 5
-
Thank you very much for your inquisitiveness to get undefined. however my intention was just to know if document.getElementById('elementx') will ever return undefined. which it will never as per these series of discussions. Now for getting undefined if we just do document.getElementById('elementx').value >>>>> it will return undefined because div does not have value. >>>>>>> please see this thread http://stackoverflow.com/questions/10694661/document-getelementbyid-value-return-undefined-in-chrome – Akshay Vijay Jain Jan 29 '15 at 09:20
-
Well, actually, once you have specified a global variable like `var undef;` (and never assign a value to it), you can use it directly anywhere you need "undefined" for something (e.g., `if(a==undef)` would be `true` if variable `a` was also undefined). You would never need to do `document.getElementById();` for anything associated with the notion of "undefined". My test-page code was more about showing a possibility than anything else. – vernonner3voltazim Jan 29 '15 at 09:30